Skip to contentSkip to conversation

hw/cxl: bound Set Feature cleanup to written extent

PATCH New Jia Jia 1 participant 1 message Apr 30

lore
Share & tools
lore

Jia Jia

author
#0 lore raw

PATCH 1/1 hw/cxl: bound Set Feature cleanup to written extent

cmd_features_set_feature() validates each fragment against the target write-attribute buffer, but it tracks data_size as the sum of every fragment copied so far.

That becomes a problem at transfer completion. Cleanup uses data_size as its memset() length, so a sequence of legal partial transfers can keep rewriting the same small range while growing data_size far beyond the feature buffer that is actually being updated.

On an ASan build, repeatedly sending 2-byte rank_sparing fragments at offset 0 and then finishing aborts the host with:

ERROR: AddressSanitizer: heap-buffer-overflow
WRITE of size 1144

      #0 __interceptor_memset
      #1 cmd_features_set_feature ../hw/cxl/cxl-mailbox-utils.c:1961
      #2 cxl_process_cci_message ../hw/cxl/cxl-mailbox-utils.c:4642
      #3 mailbox_reg_write ../hw/cxl/cxl-device-utils.c:209

Fix this by tracking the maximum written extent instead of the cumulative number of copied bytes. That keeps cleanup bounded to the actual write-attribute window without changing the rest of the transfer flow.

Add a qtest that confirms a multipart bank_sparing transfer does not clobber adjacent rank_sparing state during cleanup.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3461

---
 hw/cxl/cxl-mailbox-utils.c |  11 +---
 tests/qtest/cxl-test.c     | 119 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 121 insertions(+), 9 deletions(-)
hw/cxl/cxl-mailbox-utils.c +3 -8
index ce139e30eb..8346177b79 100644
@@ -1769,7 +1769,6 @@ static CXLRetCode cmd_features_set_feature(const struct cxl_cmd *cmd,
         memcpy((uint8_t *)&ct3d->patrol_scrub_wr_attrs + hdr->offset,
                ps_write_attrs,
                bytes_to_copy);
-        set_feat_info->data_size += bytes_to_copy;
 
         if (data_transfer_flag == CXL_SET_FEATURE_FLAG_FULL_DATA_TRANSFER ||
             data_transfer_flag ==  CXL_SET_FEATURE_FLAG_FINISH_DATA_TRANSFER) {
@@ -1796,7 +1795,6 @@ static CXLRetCode cmd_features_set_feature(const struct cxl_cmd *cmd,
         memcpy((uint8_t *)&ct3d->ecs_wr_attrs + hdr->offset,
                ecs_write_attrs,
                bytes_to_copy);
-        set_feat_info->data_size += bytes_to_copy;
 
         if (data_transfer_flag == CXL_SET_FEATURE_FLAG_FULL_DATA_TRANSFER ||
             data_transfer_flag ==  CXL_SET_FEATURE_FLAG_FINISH_DATA_TRANSFER) {
@@ -1820,7 +1818,6 @@ static CXLRetCode cmd_features_set_feature(const struct cxl_cmd *cmd,
         }
         memcpy((uint8_t *)&ct3d->soft_ppr_wr_attrs + hdr->offset,
                sppr_write_attrs, bytes_to_copy);
-        set_feat_info->data_size += bytes_to_copy;
 
         if (data_transfer_flag == CXL_SET_FEATURE_FLAG_FULL_DATA_TRANSFER ||
             data_transfer_flag ==  CXL_SET_FEATURE_FLAG_FINISH_DATA_TRANSFER) {
@@ -1842,7 +1839,6 @@ static CXLRetCode cmd_features_set_feature(const struct cxl_cmd *cmd,
         }
         memcpy((uint8_t *)&ct3d->hard_ppr_wr_attrs + hdr->offset,
                hppr_write_attrs, bytes_to_copy);
-        set_feat_info->data_size += bytes_to_copy;
 
         if (data_transfer_flag == CXL_SET_FEATURE_FLAG_FULL_DATA_TRANSFER ||
             data_transfer_flag ==  CXL_SET_FEATURE_FLAG_FINISH_DATA_TRANSFER) {
@@ -1864,7 +1860,6 @@ static CXLRetCode cmd_features_set_feature(const struct cxl_cmd *cmd,
         }
         memcpy((uint8_t *)&ct3d->cacheline_sparing_wr_attrs + hdr->offset,
                mem_sparing_write_attrs, bytes_to_copy);
-        set_feat_info->data_size += bytes_to_copy;
 
         if (data_transfer_flag == CXL_SET_FEATURE_FLAG_FULL_DATA_TRANSFER ||
             data_transfer_flag == CXL_SET_FEATURE_FLAG_FINISH_DATA_TRANSFER) {
@@ -1885,7 +1880,6 @@ static CXLRetCode cmd_features_set_feature(const struct cxl_cmd *cmd,
         }
         memcpy((uint8_t *)&ct3d->row_sparing_wr_attrs + hdr->offset,
                mem_sparing_write_attrs, bytes_to_copy);
-        set_feat_info->data_size += bytes_to_copy;
 
         if (data_transfer_flag == CXL_SET_FEATURE_FLAG_FULL_DATA_TRANSFER ||
             data_transfer_flag == CXL_SET_FEATURE_FLAG_FINISH_DATA_TRANSFER) {
@@ -1906,7 +1900,6 @@ static CXLRetCode cmd_features_set_feature(const struct cxl_cmd *cmd,
         }
         memcpy((uint8_t *)&ct3d->bank_sparing_wr_attrs + hdr->offset,
                mem_sparing_write_attrs, bytes_to_copy);
-        set_feat_info->data_size += bytes_to_copy;
 
         if (data_transfer_flag == CXL_SET_FEATURE_FLAG_FULL_DATA_TRANSFER ||
             data_transfer_flag == CXL_SET_FEATURE_FLAG_FINISH_DATA_TRANSFER) {
@@ -1927,7 +1920,6 @@ static CXLRetCode cmd_features_set_feature(const struct cxl_cmd *cmd,
         }
         memcpy((uint8_t *)&ct3d->rank_sparing_wr_attrs + hdr->offset,
                mem_sparing_write_attrs, bytes_to_copy);
-        set_feat_info->data_size += bytes_to_copy;
 
         if (data_transfer_flag == CXL_SET_FEATURE_FLAG_FULL_DATA_TRANSFER ||
             data_transfer_flag == CXL_SET_FEATURE_FLAG_FINISH_DATA_TRANSFER) {
@@ -1938,6 +1930,9 @@ static CXLRetCode cmd_features_set_feature(const struct cxl_cmd *cmd,
         return CXL_MBOX_UNSUPPORTED;
     }
 
+    set_feat_info->data_size = MAX(set_feat_info->data_size,
+                                   (size_t)end_offset);
+
     if (data_transfer_flag == CXL_SET_FEATURE_FLAG_FULL_DATA_TRANSFER ||
         data_transfer_flag ==  CXL_SET_FEATURE_FLAG_FINISH_DATA_TRANSFER ||
         data_transfer_flag ==  CXL_SET_FEATURE_FLAG_ABORT_DATA_TRANSFER) {
tests/qtest/cxl-test.c +118 -1

158 lines not on this page · raw message

Signature · 1 line
2.34.1