cxl/cdat: Fixes for CXL CDAT processing
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]>
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
@@ -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
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.
Signed-off-by Ira Weiny <[email protected]>
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@@ -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 ;
PATCH 2/2 cxl/cdat: Fix header sum value in CDAT checksum
In reply to Ira WeinyThe 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
@@ -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
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]/
Signed-off-by Ira Weiny <[email protected]>--- hw/cxl/cxl-cdat.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)hw/cxl/cxl-cdat.c +0
-0@@ -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 ;
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@@ -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