Skip to contentSkip to conversation

drm/amd: Refactor find_system_memory()

PATCH Superseded Mario Limonciello 2 participants 7 messages Feb 4, 2025 – Feb 5, 2025

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amd: Refactor find_system_memory()

From: Mario Limonciello <[email protected]>

find_system_memory() pulls out two fields from an SMBIOS type 17 device and sets them on KFD devices. This however is pulling from the middle of the field in the SMBIOS device and leads to an unaligned access.

Instead use a struct representation to access the members and pull out the two specific fields.

---
 drivers/gpu/drm/amd/amdkfd/kfd_topology.c | 27 +++++++++++------------
 drivers/gpu/drm/amd/amdkfd/kfd_topology.h | 17 ++++++++++++++
 2 files changed, 30 insertions(+), 14 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +13 -14
index ceb9fb475ef13..93d3924dfcba0 100644
@@ -968,24 +968,23 @@ static void kfd_update_system_properties(void)
 	up_read(&topology_lock);
 }
 
-static void find_system_memory(const struct dmi_header *dm,
-	void *private)
+static void find_system_memory(const struct dmi_header *dm, void *private)
 {
+	struct dmi_mem_device *memdev = (struct dmi_mem_device *)(dm);
 	struct kfd_mem_properties *mem;
-	u16 mem_width, mem_clock;
 	struct kfd_topology_device *kdev =
 		(struct kfd_topology_device *)private;
-	const u8 *dmi_data = (const u8 *)(dm + 1);
-
-	if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
-		mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
-		mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
-		list_for_each_entry(mem, &kdev->mem_props, list) {
-			if (mem_width != 0xFFFF && mem_width != 0)
-				mem->width = mem_width;
-			if (mem_clock != 0)
-				mem->mem_clk_max = mem_clock;
-		}
+
+	if (memdev->header.type != DMI_ENTRY_MEM_DEVICE)
+		return;
+	if (memdev->header.length < sizeof(struct dmi_mem_device))
+		return;
+
+	list_for_each_entry(mem, &kdev->mem_props, list) {
+		if (memdev->total_width != 0xFFFF && memdev->total_width != 0)
+			mem->width = memdev->total_width;
+		if (memdev->speed != 0)
+			mem->mem_clk_max = memdev->speed;
 	}
 }
 
drivers/gpu/drm/amd/amdkfd/kfd_topology.h +17 -0
index 155b5c410af16..f06c9db7ddde9 100644
@@ -24,6 +24,7 @@
 #ifndef __KFD_TOPOLOGY_H__
 #define __KFD_TOPOLOGY_H__
 
+#include <linux/dmi.h>
 #include <linux/types.h>
 #include <linux/list.h>
 #include <linux/kfd_sysfs.h>
@@ -179,6 +180,22 @@ struct kfd_system_properties {
 	struct attribute	attr_props;
 };
 
+struct dmi_mem_device {
+	struct dmi_header header;
+	u16 physical_handle;
+	u16 error_handle;
+	u16 total_width;
+	u16 data_width;
+	u16 size;
+	u8 form_factor;
+	u8 device_set;
+	u8 device_locator;
+	u8 bank_locator;
+	u8 memory_type;
+	u16 type_detail;
+	u16 speed;
+} __packed;
+
 struct kfd_topology_device *kfd_create_topology_device(
 		struct list_head *device_list);
 void kfd_release_topology_device_list(struct list_head *device_list);
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 9 lines · Mario Limonciello

From: Mario Limonciello <[email protected]>

find_system_memory() pulls out two fields from an SMBIOS type 17 device and sets them on KFD devices. This however is pulling from the middle of the field in the SMBIOS device and leads to an unaligned access.

Instead use a struct representation to access the members and pull out the two specific fields.

Isn't that still an unaligned access? I don't understand the purpose of this patch.

One more comment inline.

Quoted 13 lines
---
 drivers/gpu/drm/amd/amdkfd/kfd_topology.c | 27 +++++++++++------------
 drivers/gpu/drm/amd/amdkfd/kfd_topology.h | 17 ++++++++++++++
 2 files changed, 30 insertions(+), 14 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +0 -0
index ceb9fb475ef13..93d3924dfcba0 100644

drivers/gpu/drm/amd/amdkfd/kfd_topology.c static void kfd_update_system_properties(void) · line 973

Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +2 -2
@@ -968,24 +968,23 @@ static void kfd_update_system_properties(void)
 	up_read(&topology_lock);
 }
 
-static void find_system_memory(const struct dmi_header *dm,
-	void *private)
+static void find_system_memory(const struct dmi_header *dm, void *private)
 {
+	struct dmi_mem_device *memdev = (struct dmi_mem_device *)(dm);

I think it would be cleaner to use container_of to get a pointer to the structure containing the header.

Regards,
Felix

Quoted 64 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +11 -12
 	struct kfd_mem_properties *mem;
-	u16 mem_width, mem_clock;
 	struct kfd_topology_device *kdev =
 		(struct kfd_topology_device *)private;
-	const u8 *dmi_data = (const u8 *)(dm + 1);
-
-	if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
-		mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
-		mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
-		list_for_each_entry(mem, &kdev->mem_props, list) {
-			if (mem_width != 0xFFFF && mem_width != 0)
-				mem->width = mem_width;
-			if (mem_clock != 0)
-				mem->mem_clk_max = mem_clock;
-		}
+
+	if (memdev->header.type != DMI_ENTRY_MEM_DEVICE)
+		return;
+	if (memdev->header.length < sizeof(struct dmi_mem_device))
+		return;
+
+	list_for_each_entry(mem, &kdev->mem_props, list) {
+		if (memdev->total_width != 0xFFFF && memdev->total_width != 0)
+			mem->width = memdev->total_width;
+		if (memdev->speed != 0)
+			mem->mem_clk_max = memdev->speed;
 	}
 }
 
drivers/gpu/drm/amd/amdkfd/kfd_topology.h +17 -0
index 155b5c410af16..f06c9db7ddde9 100644
@@ -24,6 +24,7 @@
 #ifndef __KFD_TOPOLOGY_H__
 #define __KFD_TOPOLOGY_H__
 
+#include <linux/dmi.h>
 #include <linux/types.h>
 #include <linux/list.h>
 #include <linux/kfd_sysfs.h>
@@ -179,6 +180,22 @@ struct kfd_system_properties {
 	struct attribute	attr_props;
 };
 
+struct dmi_mem_device {
+	struct dmi_header header;
+	u16 physical_handle;
+	u16 error_handle;
+	u16 total_width;
+	u16 data_width;
+	u16 size;
+	u8 form_factor;
+	u8 device_set;
+	u8 device_locator;
+	u8 bank_locator;
+	u8 memory_type;
+	u16 type_detail;
+	u16 speed;
+} __packed;
+
 struct kfd_topology_device *kfd_create_topology_device(
 		struct list_head *device_list);
 void kfd_release_topology_device_list(struct list_head *device_list);
#2 lore raw
Quoted 9 lines · Felix Kuehling
Quoted 7 lines · Mario Limonciello

From: Mario Limonciello <[email protected]>

find_system_memory() pulls out two fields from an SMBIOS type 17 device and sets them on KFD devices. This however is pulling from the middle of the field in the SMBIOS device and leads to an unaligned access.

Quoted 4 lines
Quoted 2 lines

Instead use a struct representation to access the members and pull out the two specific fields.

Isn't that still an unaligned access? I don't understand the purpose of this patch.

Unless I added wrong, it looked to me that the offset it was pulling from previously was incorrect. So I was expecting it should be aligned (and less error prone) to pull from the correct offset from a struct.

Quoted 19 lines

One more comment inline.

Quoted 16 lines
---
  drivers/gpu/drm/amd/amdkfd/kfd_topology.c | 27 +++++++++++------------
  drivers/gpu/drm/amd/amdkfd/kfd_topology.h | 17 ++++++++++++++
  2 files changed, 30 insertions(+), 14 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +0 -0
index ceb9fb475ef13..93d3924dfcba0 100644
@@ -968,24 +968,23 @@ static void kfd_update_system_properties(void)
  	up_read(&topology_lock);
  }
 
Quoted 7 lines

drivers/gpu/drm/amd/amdkfd/kfd_topology.c static void kfd_update_system_properties(void) · line 973

Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +2 -2
-static void find_system_memory(const struct dmi_header *dm,
-	void *private)
+static void find_system_memory(const struct dmi_header *dm, void *private)
  {
+	struct dmi_mem_device *memdev = (struct dmi_mem_device *)(dm);

I think it would be cleaner to use container_of to get a pointer to the structure containing the header.

Ack.

Quoted 69 lines

Regards,
Felix

Quoted 64 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +11 -12
  	struct kfd_mem_properties *mem;
-	u16 mem_width, mem_clock;
  	struct kfd_topology_device *kdev =
  		(struct kfd_topology_device *)private;
-	const u8 *dmi_data = (const u8 *)(dm + 1);
-
-	if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
-		mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
-		mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
-		list_for_each_entry(mem, &kdev->mem_props, list) {
-			if (mem_width != 0xFFFF && mem_width != 0)
-				mem->width = mem_width;
-			if (mem_clock != 0)
-				mem->mem_clk_max = mem_clock;
-		}
+
+	if (memdev->header.type != DMI_ENTRY_MEM_DEVICE)
+		return;
+	if (memdev->header.length < sizeof(struct dmi_mem_device))
+		return;
+
+	list_for_each_entry(mem, &kdev->mem_props, list) {
+		if (memdev->total_width != 0xFFFF && memdev->total_width != 0)
+			mem->width = memdev->total_width;
+		if (memdev->speed != 0)
+			mem->mem_clk_max = memdev->speed;
  	}
  }
 
drivers/gpu/drm/amd/amdkfd/kfd_topology.h +17 -0
index 155b5c410af16..f06c9db7ddde9 100644
@@ -24,6 +24,7 @@
  #ifndef __KFD_TOPOLOGY_H__
  #define __KFD_TOPOLOGY_H__
 
+#include <linux/dmi.h>
  #include <linux/types.h>
  #include <linux/list.h>
  #include <linux/kfd_sysfs.h>
@@ -179,6 +180,22 @@ struct kfd_system_properties {
  	struct attribute	attr_props;
  };
 
+struct dmi_mem_device {
+	struct dmi_header header;
+	u16 physical_handle;
+	u16 error_handle;
+	u16 total_width;
+	u16 data_width;
+	u16 size;
+	u8 form_factor;
+	u8 device_set;
+	u8 device_locator;
+	u8 bank_locator;
+	u8 memory_type;
+	u16 type_detail;
+	u16 speed;
+} __packed;
+
  struct kfd_topology_device *kfd_create_topology_device(
  		struct list_head *device_list);
  void kfd_release_topology_device_list(struct list_head *device_list);
maintainer
#3 lore raw
Quoted 10 lines · Mario Limonciello
Quoted 9 lines · Felix Kuehling
Quoted 7 lines · Mario Limonciello

From: Mario Limonciello <[email protected]>

find_system_memory() pulls out two fields from an SMBIOS type 17 device and sets them on KFD devices. This however is pulling from the middle of the field in the SMBIOS device and leads to an unaligned access.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Instead use a struct representation to access the members and pull out the two specific fields.

Isn't that still an unaligned access? I don't understand the purpose of this patch.

Unless I added wrong, it looked to me that the offset it was pulling from previously was incorrect.  So I was expecting it should be aligned (and less error prone) to pull from the correct offset from a struct.

The way I see it, the offsets that were used before were correct and match the offsets in the packed structure definition. I'm annotating the offsets from the end of the header in the structure definition below as proof.

Quoted 69 lines
Quoted 12 lines

One more comment inline.

Quoted 8 lines

---
  drivers/gpu/drm/amd/amdkfd/kfd_topology.c | 27 +++++++++++------------ drivers/gpu/drm/amd/amdkfd/kfd_topology.h | 17 ++++++++++++++

  2 files changed, 30 insertions(+), 14 deletions(-)
Quoted 14 lines

drivers/gpu/drm/amd/amdkfd/kfd_topology.c static void kfd_update_system_properties(void) · line 973

Quoted 12 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +2 -1
index ceb9fb475ef13..93d3924dfcba0 100644
@@ -968,24 +968,23 @@ static void kfd_update_system_properties(void)
       up_read(&topology_lock);
   }
   -static void find_system_memory(const struct dmi_header *dm,
-    void *private)
+static void find_system_memory(const struct dmi_header *dm, void *private)
   {
+    struct dmi_mem_device *memdev = (struct dmi_mem_device *)(dm);

I think it would be cleaner to use container_of to get a pointer to the structure containing the header.

Ack.

Quoted 40 lines

Regards,
   Felix

Quoted 36 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +0 -1
       struct kfd_mem_properties *mem;
-    u16 mem_width, mem_clock;

      struct kfd_topology_device *kdev =
          (struct kfd_topology_device *)private;
-    const u8 *dmi_data = (const u8 *)(dm + 1);

drivers/gpu/drm/amd/amdkfd/kfd_topology.c +11 -10
-
-    if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
-        mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
-        mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
-        list_for_each_entry(mem, &kdev->mem_props, list) {
-            if (mem_width != 0xFFFF && mem_width != 0)
-                mem->width = mem_width;
-            if (mem_clock != 0)
-                mem->mem_clk_max = mem_clock;
-        }
+
+    if (memdev->header.type != DMI_ENTRY_MEM_DEVICE)
+        return;
+    if (memdev->header.length < sizeof(struct dmi_mem_device))
+        return;
+
+    list_for_each_entry(mem, &kdev->mem_props, list) {
+        if (memdev->total_width != 0xFFFF && memdev->total_width != 0)
+            mem->width = memdev->total_width;
+        if (memdev->speed != 0)
+            mem->mem_clk_max = memdev->speed;

      }
  }
  diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_topology.h b/drivers/gpu/drm/amd/amdkfd/kfd_topology.h index 155b5c410af16..f06c9db7ddde9 100644

drivers/gpu/drm/amd/amdkfd/kfd_topology.h +0 -0
@@ -24,6 +24,7 @@
   #ifndef __KFD_TOPOLOGY_H__
   #define __KFD_TOPOLOGY_H__

  +#include <linux/dmi.h>

Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/amd/amdkfd/kfd_topology.h struct kfd_system_properties { · line 183

Quoted 8 lines

  #include <linux/types.h>
  #include <linux/list.h>
  #include <linux/kfd_sysfs.h>

drivers/gpu/drm/amd/amdkfd/kfd_topology.h +1 -0
@@ -179,6 +180,22 @@ struct kfd_system_properties {
       struct attribute    attr_props;
   };
   +struct dmi_mem_device {
+    struct dmi_header header;

Comments below to annotate the byte offset of each field from the end of the header.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.h +5 -0
+    u16 physical_handle; // 0x0
+    u16 error_handle;    // 0x2
+    u16 total_width;     // 0x4
+    u16 data_width;      // 0x6 (matches the original code)
+    u16 size;            // 0x8
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/amd/amdkfd/kfd_topology.h struct kfd_system_properties { · line 196

Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.h +8 -0
+    u8 form_factor;      // 0xa
+    u8 device_set;       // 0xb
+    u8 device_locator;   // 0xc
+    u8 bank_locator;     // 0xd
+    u8 memory_type;      // 0xe
+    u16 type_detail;     // 0xf
+    u16 speed;           // 0x11 (matches the original code)
+} __packed;

The bottom line is, this patch doesn't change anything about which DMI data is accessed. It's still an unaligned access. Now I think your patch is still a decent cleanup. But the justification in the commit description doesn't make sense.

Regards,
Felix

Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.h +1 -0
+

  struct kfd_topology_device *kfd_create_topology_device( struct list_head *device_list);
  void kfd_release_topology_device_list(struct list_head *device_list);

#4 lore raw
Quoted 13 lines · Felix Kuehling
Quoted 10 lines · Mario Limonciello
Quoted 9 lines · Felix Kuehling
Quoted 7 lines · Mario Limonciello

From: Mario Limonciello <[email protected]>

find_system_memory() pulls out two fields from an SMBIOS type 17 device and sets them on KFD devices. This however is pulling from the middle of the field in the SMBIOS device and leads to an unaligned access.

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Instead use a struct representation to access the members and pull out the two specific fields.

Isn't that still an unaligned access? I don't understand the purpose of this patch.

Unless I added wrong, it looked to me that the offset it was pulling from previously was incorrect.  So I was expecting it should be aligned (and less error prone) to pull from the correct offset from a struct.

The way I see it, the offsets that were used before were correct and match the offsets in the packed structure definition. I'm annotating the offsets from the end of the header in the structure definition below as proof.

The problem I saw was that the dmi_data field starts a byte late though.
That's why I was thinking this is the source of the unaligned access
and the mistake.

Let me annotate below.

Quoted 30 lines
Quoted 30 lines
Quoted 12 lines

One more comment inline.

Quoted 8 lines
---
   drivers/gpu/drm/amd/amdkfd/kfd_topology.c | 27 +++++++++++------------
   drivers/gpu/drm/amd/amdkfd/kfd_topology.h | 17 ++++++++++++++
   2 files changed, 30 insertions(+), 14 deletions(-)
Quoted 14 lines

drivers/gpu/drm/amd/amdkfd/kfd_topology.c static void kfd_update_system_properties(void) · line 973

Quoted 12 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +2 -1
index ceb9fb475ef13..93d3924dfcba0 100644
@@ -968,24 +968,23 @@ static void kfd_update_system_properties(void)
       up_read(&topology_lock);
   }
   -static void find_system_memory(const struct dmi_header *dm,
-    void *private)
+static void find_system_memory(const struct dmi_header *dm, void *private)
   {
+    struct dmi_mem_device *memdev = (struct dmi_mem_device *)(dm);

I think it would be cleaner to use container_of to get a pointer to the structure containing the header.

Ack.

Quoted 1 line
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Regards,
   Felix

Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +0 -2
       struct kfd_mem_properties *mem;
-    u16 mem_width, mem_clock;
       struct kfd_topology_device *kdev =
           (struct kfd_topology_device *)private;
-    const u8 *dmi_data = (const u8 *)(dm + 1);
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 68 lines
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +11 -10
-
-    if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
-        mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
-        mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
-        list_for_each_entry(mem, &kdev->mem_props, list) {
-            if (mem_width != 0xFFFF && mem_width != 0)
-                mem->width = mem_width;
-            if (mem_clock != 0)
-                mem->mem_clk_max = mem_clock;
-        }
+
+    if (memdev->header.type != DMI_ENTRY_MEM_DEVICE)
+        return;
+    if (memdev->header.length < sizeof(struct dmi_mem_device))
+        return;
+
+    list_for_each_entry(mem, &kdev->mem_props, list) {
+        if (memdev->total_width != 0xFFFF && memdev->total_width != 0)
+            mem->width = memdev->total_width;
+        if (memdev->speed != 0)
+            mem->mem_clk_max = memdev->speed;
       }
   }
   diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_topology.h b/drivers/gpu/drm/amd/amdkfd/kfd_topology.h

index 155b5c410af16..f06c9db7ddde9 100644

drivers/gpu/drm/amd/amdkfd/kfd_topology.h +0 -0
@@ -24,6 +24,7 @@
   #ifndef __KFD_TOPOLOGY_H__
   #define __KFD_TOPOLOGY_H__
   +#include <linux/dmi.h>
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/amd/amdkfd/kfd_topology.h struct kfd_system_properties { · line 183

Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.h +1 -0
   #include <linux/types.h>
   #include <linux/list.h>
   #include <linux/kfd_sysfs.h>
@@ -179,6 +180,22 @@ struct kfd_system_properties {
       struct attribute    attr_props;
   };
   +struct dmi_mem_device {
+    struct dmi_header header;

Comments below to annotate the byte offset of each field from the end of the header.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.h +5 -0
+    u16 physical_handle; // 0x0
+    u16 error_handle;    // 0x2
+    u16 total_width;     // 0x4
+    u16 data_width;      // 0x6 (matches the original code)
+    u16 size;            // 0x8
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/amd/amdkfd/kfd_topology.h struct kfd_system_properties { · line 196

Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.h +8 -0
+    u8 form_factor;      // 0xa
+    u8 device_set;       // 0xb
+    u8 device_locator;   // 0xc
+    u8 bank_locator;     // 0xd
+    u8 memory_type;      // 0xe
+    u16 type_detail;     // 0xf
+    u16 speed;           // 0x11 (matches the original code)
+} __packed;

The bottom line is, this patch doesn't change anything about which DMI data is accessed. It's still an unaligned access. Now I think your patch is still a decent cleanup. But the justification in the commit description doesn't make sense.

Regards,
Felix

Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.h +1 -0
+
   struct kfd_topology_device *kfd_create_topology_device(
           struct list_head *device_list);
   void kfd_release_topology_device_list(struct list_head *device_list);
maintainer
#5 lore raw
Quoted 61 lines · Mario Limonciello
Quoted 13 lines · Felix Kuehling
Quoted 10 lines · Mario Limonciello
Quoted 9 lines · Felix Kuehling
Quoted 7 lines · Mario Limonciello

From: Mario Limonciello <[email protected]>

find_system_memory() pulls out two fields from an SMBIOS type 17 device and sets them on KFD devices. This however is pulling from the middle of the field in the SMBIOS device and leads to an unaligned access.

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Instead use a struct representation to access the members and pull out the two specific fields.

Isn't that still an unaligned access? I don't understand the purpose of this patch.

Unless I added wrong, it looked to me that the offset it was pulling from previously was incorrect.  So I was expecting it should be aligned (and less error prone) to pull from the correct offset from a struct.

The way I see it, the offsets that were used before were correct and match the offsets in the packed structure definition. I'm annotating the offsets from the end of the header in the structure definition below as proof.

The problem I saw was that the dmi_data field starts a byte late though.  That's why I was thinking this is the source of the unaligned access and the mistake.

Let me annotate below.

Quoted 33 lines
Quoted 33 lines
Quoted 12 lines

One more comment inline.

Quoted 8 lines

---
   drivers/gpu/drm/amd/amdkfd/kfd_topology.c | 27 +++++++++++------------ drivers/gpu/drm/amd/amdkfd/kfd_topology.h | 17 ++++++++++++++

   2 files changed, 30 insertions(+), 14 deletions(-)
Quoted 14 lines

drivers/gpu/drm/amd/amdkfd/kfd_topology.c static void kfd_update_system_properties(void) · line 973

Quoted 12 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +2 -1
index ceb9fb475ef13..93d3924dfcba0 100644
@@ -968,24 +968,23 @@ static void kfd_update_system_properties(void)
        up_read(&topology_lock);
    }
    -static void find_system_memory(const struct dmi_header *dm,
-    void *private)
+static void find_system_memory(const struct dmi_header *dm, void *private)
    {
+    struct dmi_mem_device *memdev = (struct dmi_mem_device *)(dm);

I think it would be cleaner to use container_of to get a pointer to the structure containing the header.

Ack.

Quoted 4 lines

Regards,
    Felix

Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +0 -1
        struct kfd_mem_properties *mem;
-    u16 mem_width, mem_clock;

       struct kfd_topology_device *kdev =
           (struct kfd_topology_device *)private;
-    const u8 *dmi_data = (const u8 *)(dm + 1);

          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The type of dm is struct dmi_header *. dm + 1 is the first byte after the dmi_header. In other words dmi_data points to the physical_handle field, which is the field with offset 0 as I annotated it below and all the other offsets match my annotations, which match the original code.

Regards,
Felix

Quoted 69 lines
Quoted 68 lines
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +11 -10
-
-    if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
-        mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
-        mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
-        list_for_each_entry(mem, &kdev->mem_props, list) {
-            if (mem_width != 0xFFFF && mem_width != 0)
-                mem->width = mem_width;
-            if (mem_clock != 0)
-                mem->mem_clk_max = mem_clock;
-        }
+
+    if (memdev->header.type != DMI_ENTRY_MEM_DEVICE)
+        return;
+    if (memdev->header.length < sizeof(struct dmi_mem_device))
+        return;
+
+    list_for_each_entry(mem, &kdev->mem_props, list) {
+        if (memdev->total_width != 0xFFFF && memdev->total_width != 0)
+            mem->width = memdev->total_width;
+        if (memdev->speed != 0)
+            mem->mem_clk_max = memdev->speed;

       }
   }
   diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_topology.h b/drivers/gpu/drm/amd/amdkfd/kfd_topology.h index 155b5c410af16..f06c9db7ddde9 100644

drivers/gpu/drm/amd/amdkfd/kfd_topology.h +0 -0
@@ -24,6 +24,7 @@
    #ifndef __KFD_TOPOLOGY_H__
    #define __KFD_TOPOLOGY_H__

   +#include <linux/dmi.h>

Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/amd/amdkfd/kfd_topology.h struct kfd_system_properties { · line 183

Quoted 8 lines

   #include <linux/types.h>
   #include <linux/list.h>
   #include <linux/kfd_sysfs.h>

drivers/gpu/drm/amd/amdkfd/kfd_topology.h +1 -0
@@ -179,6 +180,22 @@ struct kfd_system_properties {
        struct attribute    attr_props;
    };
    +struct dmi_mem_device {
+    struct dmi_header header;

Comments below to annotate the byte offset of each field from the end of the header.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.h +5 -0
+    u16 physical_handle; // 0x0
+    u16 error_handle;    // 0x2
+    u16 total_width;     // 0x4
+    u16 data_width;      // 0x6 (matches the original code)
+    u16 size;            // 0x8
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/amd/amdkfd/kfd_topology.h struct kfd_system_properties { · line 196

Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.h +8 -0
+    u8 form_factor;      // 0xa
+    u8 device_set;       // 0xb
+    u8 device_locator;   // 0xc
+    u8 bank_locator;     // 0xd
+    u8 memory_type;      // 0xe
+    u16 type_detail;     // 0xf
+    u16 speed;           // 0x11 (matches the original code)
+} __packed;

The bottom line is, this patch doesn't change anything about which DMI data is accessed. It's still an unaligned access. Now I think your patch is still a decent cleanup. But the justification in the commit description doesn't make sense.

Regards,
   Felix

Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.h +1 -0
+

   struct kfd_topology_device *kfd_create_topology_device( struct list_head *device_list);
   void kfd_release_topology_device_list(struct list_head *device_list);

#6 lore raw
Quoted 64 lines · Felix Kuehling
Quoted 61 lines · Mario Limonciello
Quoted 13 lines · Felix Kuehling
Quoted 10 lines · Mario Limonciello
Quoted 9 lines · Felix Kuehling
Quoted 7 lines · Mario Limonciello

From: Mario Limonciello <[email protected]>

find_system_memory() pulls out two fields from an SMBIOS type 17 device and sets them on KFD devices. This however is pulling from the middle of the field in the SMBIOS device and leads to an unaligned access.

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Instead use a struct representation to access the members and pull out the two specific fields.

Isn't that still an unaligned access? I don't understand the purpose of this patch.

Unless I added wrong, it looked to me that the offset it was pulling from previously was incorrect.  So I was expecting it should be aligned (and less error prone) to pull from the correct offset from a struct.

The way I see it, the offsets that were used before were correct and match the offsets in the packed structure definition. I'm annotating the offsets from the end of the header in the structure definition below as proof.

The problem I saw was that the dmi_data field starts a byte late though.  That's why I was thinking this is the source of the unaligned access and the mistake.

Let me annotate below.

Quoted 33 lines
Quoted 33 lines
Quoted 12 lines

One more comment inline.

Quoted 8 lines
---
    drivers/gpu/drm/amd/amdkfd/kfd_topology.c | 27 +++++++++++------------
    drivers/gpu/drm/amd/amdkfd/kfd_topology.h | 17 ++++++++++++++
    2 files changed, 30 insertions(+), 14 deletions(-)
Quoted 14 lines

drivers/gpu/drm/amd/amdkfd/kfd_topology.c static void kfd_update_system_properties(void) · line 973

Quoted 12 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +2 -1
index ceb9fb475ef13..93d3924dfcba0 100644
@@ -968,24 +968,23 @@ static void kfd_update_system_properties(void)
        up_read(&topology_lock);
    }
    -static void find_system_memory(const struct dmi_header *dm,
-    void *private)
+static void find_system_memory(const struct dmi_header *dm, void *private)
    {
+    struct dmi_mem_device *memdev = (struct dmi_mem_device *)(dm);

I think it would be cleaner to use container_of to get a pointer to the structure containing the header.

Ack.

Quoted 4 lines

Regards,
    Felix

Quoted 8 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/gpu/drm/amd/amdkfd/kfd_topology.c static void kfd_update_system_properties(void) · line 978

Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +0 -2
        struct kfd_mem_properties *mem;
-    u16 mem_width, mem_clock;
        struct kfd_topology_device *kdev =
            (struct kfd_topology_device *)private;
-    const u8 *dmi_data = (const u8 *)(dm + 1);

          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The type of dm is struct dmi_header *. dm + 1 is the first byte after the dmi_header. In other words dmi_data points to the physical_handle field, which is the field with offset 0 as I annotated it below and all the other offsets match my annotations, which match the original code.

Ah got it, I understand now. I'll adjust the commit message as you're right this doesn't change anything for unaligned access and send out a v2 just as just being a pure cleanup.

Quoted 74 lines

Regards,
Felix

Quoted 69 lines
Quoted 68 lines
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +11 -10
-
-    if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
-        mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
-        mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
-        list_for_each_entry(mem, &kdev->mem_props, list) {
-            if (mem_width != 0xFFFF && mem_width != 0)
-                mem->width = mem_width;
-            if (mem_clock != 0)
-                mem->mem_clk_max = mem_clock;
-        }
+
+    if (memdev->header.type != DMI_ENTRY_MEM_DEVICE)
+        return;
+    if (memdev->header.length < sizeof(struct dmi_mem_device))
+        return;
+
+    list_for_each_entry(mem, &kdev->mem_props, list) {
+        if (memdev->total_width != 0xFFFF && memdev->total_width != 0)
+            mem->width = memdev->total_width;
+        if (memdev->speed != 0)
+            mem->mem_clk_max = memdev->speed;
        }
    }
    diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_topology.h b/drivers/gpu/drm/amd/amdkfd/kfd_topology.h

index 155b5c410af16..f06c9db7ddde9 100644

drivers/gpu/drm/amd/amdkfd/kfd_topology.h +0 -0
@@ -24,6 +24,7 @@
    #ifndef __KFD_TOPOLOGY_H__
    #define __KFD_TOPOLOGY_H__
    +#include <linux/dmi.h>
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/amd/amdkfd/kfd_topology.h struct kfd_system_properties { · line 183

Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.h +1 -0
    #include <linux/types.h>
    #include <linux/list.h>
    #include <linux/kfd_sysfs.h>
@@ -179,6 +180,22 @@ struct kfd_system_properties {
        struct attribute    attr_props;
    };
    +struct dmi_mem_device {
+    struct dmi_header header;

Comments below to annotate the byte offset of each field from the end of the header.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.h +5 -0
+    u16 physical_handle; // 0x0
+    u16 error_handle;    // 0x2
+    u16 total_width;     // 0x4
+    u16 data_width;      // 0x6 (matches the original code)
+    u16 size;            // 0x8
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/amd/amdkfd/kfd_topology.h struct kfd_system_properties { · line 196

Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.h +8 -0
+    u8 form_factor;      // 0xa
+    u8 device_set;       // 0xb
+    u8 device_locator;   // 0xc
+    u8 bank_locator;     // 0xd
+    u8 memory_type;      // 0xe
+    u16 type_detail;     // 0xf
+    u16 speed;           // 0x11 (matches the original code)
+} __packed;

The bottom line is, this patch doesn't change anything about which DMI data is accessed. It's still an unaligned access. Now I think your patch is still a decent cleanup. But the justification in the commit description doesn't make sense.

Regards,
   Felix

Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_topology.h +1 -0
+
    struct kfd_topology_device *kfd_create_topology_device(
            struct list_head *device_list);
    void kfd_release_topology_device_list(struct list_head *device_list);