Skip to contentSkip to conversation

cxl/cdat: Fixes for CXL CDAT processing

PATCH Superseded Ira Weiny 2 participants 6 messages Nov 18, 2023 – Nov 27, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ira Weiny

author
#0 lore raw

cover cxl/cdat: Fixes for CXL CDAT processing

These are a couple of fixes needed for DCD processing. The first is via code inspection and the second was found as a result of the kernel bug found previously.[1]

Does the QEMU code require fixes tags like the kernel?

[1] https://lore.kernel.org/all/[email protected]/

---
Ira Weiny (2):
cxl/cdat: Handle cdat table build errors cxl/cdat: Fix header sum value in CDAT checksum

 hw/cxl/cxl-cdat.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

---
base-commit: abe893944bb3fb3ca59aaeaa8d48e52dfdc0f3db
change-id: 20231117-fix-cdat-cs-a8ed72ec2244

Best regards,

Signature · 1 line
Ira Weiny <[email protected]>
author
#1 lore raw

PATCH 1/2 cxl/cdat: Handle cdat table build errors

The callback for building CDAT tables may return negative error codes. This was previously unhandled and will result in potentially huge allocations later on in ct3_build_cdat()

Detect the negative error code and defer cdat building.

---
This likely needs to have some more robust error handling in the event of errors. But this at least prevents more errors down the line with invalid allocations.

---
 hw/cxl/cxl-cdat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/cxl/cxl-cdat.c +1 -1
index 639a2db3e17b..24829cf2428d 100644
@@ -63,7 +63,7 @@ static void ct3_build_cdat(CDATObject *cdat, Error **errp)
     cdat->built_buf_len = cdat->build_cdat_table(&cdat->built_buf,
                                                  cdat->private);
 
-    if (!cdat->built_buf_len) {
+    if (cdat->built_buf_len <= 0) {
         /* Build later as not all data available yet */
         cdat->to_update = true;
         return;
Signature · 1 line
2.41.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] cxl/cdat: Handle cdat table build errors

Quoted 7 lines · Ira Weiny

The callback for building CDAT tables may return negative error codes. This was previously unhandled and will result in potentially huge allocations later on in ct3_build_cdat()

Detect the negative error code and defer cdat building.

Quoted 23 lines

---
This likely needs to have some more robust error handling in the event of errors. But this at least prevents more errors down the line with invalid allocations.

---
 hw/cxl/cxl-cdat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/cxl/cxl-cdat.c +1 -1
index 639a2db3e17b..24829cf2428d 100644
@@ -63,7 +63,7 @@ static void ct3_build_cdat(CDATObject *cdat, Error **errp)
     cdat->built_buf_len = cdat->build_cdat_table(&cdat->built_buf,
                                                  cdat->private);
 
-    if (!cdat->built_buf_len) {
+    if (cdat->built_buf_len <= 0) {
         /* Build later as not all data available yet */
         cdat->to_update = true;
         return;
author
#3 lore raw

PATCH 2/2 cxl/cdat: Fix header sum value in CDAT checksum

In reply to Ira Weiny

The addition of the DCD support for CXL type-3 devices extended the CDAT table large enough that the checksum being returned was incorrect.[1]

This was because the checksum value was using the header length field rather than each of the 4 bytes of the length field. This was previously not seen because the length of the CDAT data was less than 256 thus resulting in an equivalent checksum value.

Properly calculate the checksum for the CDAT header.

[1] https://lore.kernel.org/all/[email protected]/

---
 hw/cxl/cxl-cdat.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
hw/cxl/cxl-cdat.c +9 -2
index 24829cf2428d..d93e2e4e64f2 100644
@@ -95,8 +95,15 @@ static void ct3_build_cdat(CDATObject *cdat, Error **errp)
     /* For now, no runtime updates */
     cdat_header->sequence = 0;
     cdat_header->length += sizeof(CDATTableHeader);
-    sum += cdat_header->revision + cdat_header->sequence +
-        cdat_header->length;
+
+    do {
+        uint8_t *buf = (uint8_t *)cdat_header;
+
+        for (i = 0; i < sizeof(*cdat_header); i++) {
+            sum += buf[i];
+        }
+    } while (0);
+
     /* Sum of all bytes including checksum must be 0 */
     cdat_header->checksum = ~sum + 1;
 
Signature · 1 line
2.41.0
#4 lore raw

Re: [PATCH 2/2] cxl/cdat: Fix header sum value in CDAT checksum

Quoted 25 lines · Ira Weiny

The addition of the DCD support for CXL type-3 devices extended the CDAT table large enough that the checksum being returned was incorrect.[1]

This was because the checksum value was using the header length field rather than each of the 4 bytes of the length field. This was previously not seen because the length of the CDAT data was less than 256 thus resulting in an equivalent checksum value.

Properly calculate the checksum for the CDAT header.

[1] https://lore.kernel.org/all/[email protected]/

---
 hw/cxl/cxl-cdat.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
hw/cxl/cxl-cdat.c +0 -0
index 24829cf2428d..d93e2e4e64f2 100644
@@ -95,8 +95,15 @@ static void ct3_build_cdat(CDATObject *cdat, Error **errp)
     /* For now, no runtime updates */
     cdat_header->sequence = 0;
     cdat_header->length += sizeof(CDATTableHeader);

hw/cxl/cxl-cdat.c static void ct3_build_cdat(CDATObject *cdat, Error **errp) · line 105

Quoted 10 lines
hw/cxl/cxl-cdat.c +8 -2
-    sum += cdat_header->revision + cdat_header->sequence +
-        cdat_header->length;
+
+    do {
+        uint8_t *buf = (uint8_t *)cdat_header;
+
+        for (i = 0; i < sizeof(*cdat_header); i++) {
+            sum += buf[i];
+        }
+    } while (0);

Why the empty do/while loop?

Quoted 5 lines
hw/cxl/cxl-cdat.c +1 -0
+
     /* Sum of all bytes including checksum must be 0 */
     cdat_header->checksum = ~sum + 1;
 
author
#5 lore raw

Re: [PATCH 2/2] cxl/cdat: Fix header sum value in CDAT checksum

Quoted 1 line · Dave Jiang

[snip]

Quoted 12 lines
Quoted 12 lines
hw/cxl/cxl-cdat.c +2 -2
index 24829cf2428d..d93e2e4e64f2 100644
@@ -95,8 +95,15 @@ static void ct3_build_cdat(CDATObject *cdat, Error **errp)
     /* For now, no runtime updates */
     cdat_header->sequence = 0;
     cdat_header->length += sizeof(CDATTableHeader);
-    sum += cdat_header->revision + cdat_header->sequence +
-        cdat_header->length;
+
+    do {
Quoted 8 lines

hw/cxl/cxl-cdat.c static void ct3_build_cdat(CDATObject *cdat, Error **errp) · line 105

Quoted 6 lines
hw/cxl/cxl-cdat.c +6 -0
+        uint8_t *buf = (uint8_t *)cdat_header;
+
+        for (i = 0; i < sizeof(*cdat_header); i++) {
+            sum += buf[i];
+        }
+    } while (0);

Why the empty do/while loop?

Because I used the loop for debugging and forgot to clean up after it was tested.

I'll send a v2,
Ira