Skip to contentSkip to conversation

cxl: Multi-headed Single Logical Device (MHSLD)

RFC PATCH In review Gregory Price 2 participants 11 messages Oct 18, 2024 – Jan 24, 2025 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

v3 cover cxl: Multi-headed Single Logical Device (MHSLD)

MHSLDs allow multiple hosts to access dynamic capacity on a single backing device. This complicates DC management because adds, removals, and accesses need to be vetted such that hosts don't stomp on each other's data.

This patchset proposes a set of hooks to be called in cxl_type3.c when each of the above events happens. The results of the hooks can be used to prevent illegal DC operations in the corresponding cxl_* functions.

It also proposes an implementation for MHSLD emulation. The device's state is stored in a shared memory-backed file (on Linux, under /dev/shm). Extent ownership is tracked in a bytemap, wherein every byte acts as a set of flags showing which host ID can access the extent at that index. Blocks in an extent are claimed via compare-and-swap. Failure to claim the entirety of an extent is treated as a failure; the host will parse back over any blocks claimed so far and mask out its corresponding ownership bit. Operations on block states are done via atomics. This allows claims on unrelated extents to be serviced concurrently.

Block state is relegated to a u8 to conserve space. Therefore only 8 heads are currently supported.

The MHSLD device inherits from the CXL_TYPE3 class and adds the following configuration options:
--mhd-head=<u32>
--mhd-state_file=<str>
--mhd-init=<bool>

--mhd-head specifies the head ID of the host on the given device.

--mhd-state_file is the name of the shared-memory-backed file used to store the MHD state.

--mhd-init indicates whether this QEMU instance should initialize the state_file; if so, the instance will create the file if it does not exist, ftruncate it to the appropriate size, and initialize its header. It is assumed that the --mhd-init instance is run and allowed to completely finish configuration before any other guests access the shared state.

The shared state file only needs to be intialized once. Even if a guest dies without clearing the ownership bits associated with its head-ID, future guests with that ID will clear those bits in cxl_mhsld_realize(), regardless of whether mhd_init is true or false.

The following command line options create an MHSLD with 4GB of backing memory, whose state is tracked in /dev/shm/mhd_metadata. --mhd-init=true tells this instance to initialize the file as described above.

./qemu-system_x86-64 \
[... other options ...] \
-device pxb-cxl,id=cxl.0,bus=pcie.0,bus_nr=52 \
-device cxl-rp,id=rp0,bus=cxl.0,chassis=0,port=0,slot=0 \
-object memory-backend-ram,id=mem0,size=4G \
-device cxl-mhsld,bus=rp0,num-dc-regions=1,volatile-dc-memdev=mem0,id=cxl-mem0,sn=66667,mhd-head=0,mhd-state_file=mhd_metadata,mhd-init=true \
-M cxl-fmw.0.targets.0=cxl.0,cxl-fmw.0.size=4G \
-qmp unix:/tmp/qmp-sock-1,server,nowait

Once this guest completes setup, other guests looking to access the device can be booted with the same configuration options, but with --mhd-head != 0,
--mhd-init=false,
and a different QMP socket.

The first patch in the series moves the CXLUpdateDCExtentListInPl struct to a shared header. This allows DC-management code outside of cxl_type3.c (like mhsld.c) to see which extents have been accepted by a host in its DC-add response.

Changes in v3:
- rebase on 10_15_2024 branch, handle renaming issues
- dc->reset to legacy_reset update

Changes in v2:
- remove explicit init_mhsld script
- implement remaining DC add/remove logic

---
Svetly Todorov (3):
cxl-mailbox-utils: move CXLUpdateDCExtentListInPl into header
cxl_type3: add MHD callbacks
mhsld: implement MHSLD device

 hw/cxl/Kconfig              |   1 +
 hw/cxl/cxl-mailbox-utils.c  |  44 ++--
 hw/cxl/meson.build          |   1 +
 hw/cxl/mhsld/Kconfig        |   4 +
 hw/cxl/mhsld/meson.build    |   3 +
 hw/cxl/mhsld/mhsld.c        | 456 ++++++++++++++++++++++++++++++++++++
 hw/cxl/mhsld/mhsld.h        |  75 ++++++
 hw/mem/cxl_type3.c          |  17 ++
 include/hw/cxl/cxl_device.h |  24 ++
 9 files changed, 608 insertions(+), 17 deletions(-)
 create mode 100644 hw/cxl/mhsld/Kconfig
 create mode 100644 hw/cxl/mhsld/meson.build
 create mode 100644 hw/cxl/mhsld/mhsld.c
 create mode 100644 hw/cxl/mhsld/mhsld.h

---
base-commit: c4f85d1b04caf0a2baeccb13790da5e2a42fb0d9

Signature · 1 line
2.43.0
#1 lore raw

RFC PATCH v3 1/3 cxl-mailbox-utils: move CXLUpdateDCExtentListInPl into header

From: Svetly Todorov <[email protected]>

Allows other CXL devices to access host DCD-add-response payload.

---
 hw/cxl/cxl-mailbox-utils.c  | 16 ----------------
 include/hw/cxl/cxl_device.h | 16 ++++++++++++++++
 2 files changed, 16 insertions(+), 16 deletions(-)
hw/cxl/cxl-mailbox-utils.c +0 -16
index 72c03d85cf..10de26605c 100644
@@ -2446,22 +2446,6 @@ void cxl_extent_group_list_delete_front(CXLDCExtentGroupList *list)
     g_free(group);
 }
 
-/*
- * CXL r3.1 Table 8-168: Add Dynamic Capacity Response Input Payload
- * CXL r3.1 Table 8-170: Release Dynamic Capacity Input Payload
- */
-typedef struct CXLUpdateDCExtentListInPl {
-    uint32_t num_entries_updated;
-    uint8_t flags;
-    uint8_t rsvd[3];
-    /* CXL r3.1 Table 8-169: Updated Extent */
-    struct {
-        uint64_t start_dpa;
-        uint64_t len;
-        uint8_t rsvd[8];
-    } QEMU_PACKED updated_entries[];
-} QEMU_PACKED CXLUpdateDCExtentListInPl;
-
 /*
  * For the extents in the extent list to operate, check whether they are valid
  * 1. The extent should be in the range of a valid DC region;
include/hw/cxl/cxl_device.h +16 -0
index c3e93b876a..b2dc7fb769 100644
@@ -552,6 +552,22 @@ typedef struct CXLDCExtentGroup {
 } CXLDCExtentGroup;
 typedef QTAILQ_HEAD(, CXLDCExtentGroup) CXLDCExtentGroupList;
 
+/*
+ * CXL r3.1 Table 8-168: Add Dynamic Capacity Response Input Payload
+ * CXL r3.1 Table 8-170: Release Dynamic Capacity Input Payload
+ */
+typedef struct CXLUpdateDCExtentListInPl {
+    uint32_t num_entries_updated;
+    uint8_t flags;
+    uint8_t rsvd[3];
+    /* CXL r3.1 Table 8-169: Updated Extent */
+    struct {
+        uint64_t start_dpa;
+        uint64_t len;
+        uint8_t rsvd[8];
+    } QEMU_PACKED updated_entries[];
+} QEMU_PACKED CXLUpdateDCExtentListInPl;
+
 typedef struct CXLDCRegion {
     uint64_t base;       /* aligned to 256*MiB */
     uint64_t decode_len; /* aligned to 256*MiB */
Signature · 1 line
2.43.0
#2 lore raw

RFC PATCH v3 2/3 cxl_type3: add MHD callbacks

In reply to Gregory Price

From: Svetly Todorov <[email protected]>

Introduce an API for validating DC adds, removes, and responses against a multi-headed device.

mhd_reserve_extents() is called during a DC add request. This allows a multi-headed device to check whether the requested extents belong to another host. If not, then this function can claim those extents in the MHD state and allow the cxl_type3 code to follow suit in the host-local blk_bitmap.

mhd_reclaim_extents() is called during the DC add response. It allows the MHD to reclaim extents that were preallocated to a host during the request but rejected in the response.

mhd_release_extent() is called during the DC release response. It can be invoked after a host frees an extent in its local bitmap, allowing the MHD handler to release that same extent in the multi-host state.

---
 hw/cxl/cxl-mailbox-utils.c  | 28 +++++++++++++++++++++++++++-
 hw/mem/cxl_type3.c          | 17 +++++++++++++++++
 include/hw/cxl/cxl_device.h |  8 ++++++++
 3 files changed, 52 insertions(+), 1 deletion(-)
hw/cxl/cxl-mailbox-utils.c +27 -1
index 10de26605c..112272e9ac 100644
@@ -2545,6 +2545,7 @@ static CXLRetCode cmd_dcd_add_dyn_cap_rsp(const struct cxl_cmd *cmd,
 {
     CXLUpdateDCExtentListInPl *in = (void *)payload_in;
     CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
+    CXLType3Class *cvc = CXL_TYPE3_GET_CLASS(ct3d);
     CXLDCExtentList *extent_list = &ct3d->dc.extents;
     uint32_t i;
     uint64_t dpa, len;
@@ -2579,6 +2580,11 @@ static CXLRetCode cmd_dcd_add_dyn_cap_rsp(const struct cxl_cmd *cmd,
         ct3d->dc.total_extent_count += 1;
         ct3_set_region_block_backed(ct3d, dpa, len);
     }
+
+    if (cvc->mhd_reclaim_extents)
+        cvc->mhd_reclaim_extents(&ct3d->parent_obj, &ct3d->dc.extents_pending,
+                in);
+
     /* Remove the first extent group in the pending list */
     cxl_extent_group_list_delete_front(&ct3d->dc.extents_pending);
 
@@ -2612,6 +2618,7 @@ static CXLRetCode cxl_dc_extent_release_dry_run(CXLType3Dev *ct3d,
         uint32_t *updated_list_size)
 {
     CXLDCExtent *ent, *ent_next;
+    CXLType3Class *cvc = CXL_TYPE3_GET_CLASS(ct3d);
     uint64_t dpa, len;
     uint32_t i;
     int cnt_delta = 0;
@@ -2632,6 +2639,13 @@ static CXLRetCode cxl_dc_extent_release_dry_run(CXLType3Dev *ct3d,
             goto free_and_exit;
         }
 
+        /* In an MHD, check that this DPA range belongs to this host */
+        if (cvc->mhd_access_valid &&
+            !cvc->mhd_access_valid(&ct3d->parent_obj, dpa, len)) {
+            ret = CXL_MBOX_INVALID_PA;
+            goto free_and_exit;
+        }
+
         /* After this point, extent overflow is the only error can happen */
         while (len > 0) {
             QTAILQ_FOREACH(ent, updated_list, node) {
@@ -2704,9 +2718,11 @@ static CXLRetCode cmd_dcd_release_dyn_cap(const struct cxl_cmd *cmd,
 {
     CXLUpdateDCExtentListInPl *in = (void *)payload_in;
     CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
+    CXLType3Class *cvc = CXL_TYPE3_GET_CLASS(ct3d);
     CXLDCExtentList updated_list;
     CXLDCExtent *ent, *ent_next;
-    uint32_t updated_list_size;
+    uint32_t updated_list_size, i;
+    uint64_t dpa, len;
     CXLRetCode ret;
 
     if (in->num_entries_updated == 0) {
@@ -2724,6 +2740,16 @@ static CXLRetCode cmd_dcd_release_dyn_cap(const struct cxl_cmd *cmd,
         return ret;
     }
 
+    /* Updated_entries contains the released extents. Free those in the MHD */
+    for (i = 0; cvc->mhd_release_extent && i < in->num_entries_updated; ++i) {
+        dpa = in->updated_entries[i].start_dpa;
+        len = in->updated_entries[i].len;
+
+        if (cvc->mhd_release_extent) {
+            cvc->mhd_release_extent(&ct3d->parent_obj, dpa, len);
+        }
+    }
+
     /*
      * If the dry run release passes, the returned updated_list will
      * be the updated extent list and we just need to clear the extents
hw/mem/cxl_type3.c +17 -0
index b7b24b6a32..a94b9931d2 100644
@@ -799,6 +799,7 @@ static void cxl_destroy_dc_regions(CXLType3Dev *ct3d)
 {
     CXLDCExtent *ent, *ent_next;
     CXLDCExtentGroup *group, *group_next;
+    CXLType3Class *cvc = CXL_TYPE3_CLASS(ct3d);
     int i;
     CXLDCRegion *region;
 
@@ -817,6 +818,10 @@ static void cxl_destroy_dc_regions(CXLType3Dev *ct3d)
     for (i = 0; i < ct3d->dc.num_regions; i++) {
         region = &ct3d->dc.regions[i];
         g_free(region->blk_bitmap);
+        if (cvc->mhd_release_extent) {
+            cvc->mhd_release_extent(&ct3d->parent_obj, region->base,
+                    region->len);
+        }
     }
 }
 
@@ -2077,6 +2082,7 @@ static void qmp_cxl_process_dynamic_capacity_prescriptive(const char *path,
     CXLEventDynamicCapacity dCap = {};
     CXLEventRecordHdr *hdr = &dCap.hdr;
     CXLType3Dev *dcd;
+    CXLType3Class *cvc;
     uint8_t flags = 1 << CXL_EVENT_TYPE_INFO;
     uint32_t num_extents = 0;
     CxlDynamicCapacityExtentList *list;
@@ -2094,6 +2100,7 @@ static void qmp_cxl_process_dynamic_capacity_prescriptive(const char *path,
     }
 
     dcd = CXL_TYPE3(obj);
+    cvc = CXL_TYPE3_GET_CLASS(dcd);
     if (!dcd->dc.num_regions) {
         error_setg(errp, "No dynamic capacity support from the device");
         return;
@@ -2166,6 +2173,13 @@ static void qmp_cxl_process_dynamic_capacity_prescriptive(const char *path,
         num_extents++;
     }
 
+    /* If this is an MHD, attempt to reserve the extents */
+    if (type == DC_EVENT_ADD_CAPACITY && cvc->mhd_reserve_extents &&
+       !cvc->mhd_reserve_extents(&dcd->parent_obj, records, rid)) {
+        error_setg(errp, "mhsld is enabled and extent reservation failed");
+        return;
+    }
+
     /* Create extent list for event being passed to host */
     i = 0;
     list = records;
@@ -2304,6 +2318,9 @@ static void ct3_class_init(ObjectClass *oc, void *data)
     cvc->set_cacheline = set_cacheline;
     cvc->mhd_get_info = NULL;
     cvc->mhd_access_valid = NULL;
+    cvc->mhd_reserve_extents = NULL;
+    cvc->mhd_reclaim_extents = NULL;
+    cvc->mhd_release_extent = NULL;
 }
 
 static const TypeInfo ct3d_info = {
include/hw/cxl/cxl_device.h +8 -0
index b2dc7fb769..13c97b576f 100644
@@ -14,6 +14,7 @@
 #include "hw/pci/pci_device.h"
 #include "hw/register.h"
 #include "hw/cxl/cxl_events.h"
+#include "qapi/qapi-commands-cxl.h"
 
 #include "hw/cxl/cxl_cpmu.h"
 /*
@@ -682,6 +683,13 @@ struct CXLType3Class {
                                size_t *len_out,
                                CXLCCI *cci);
     bool (*mhd_access_valid)(PCIDevice *d, uint64_t addr, unsigned int size);
+    bool (*mhd_reserve_extents)(PCIDevice *d,
+                                CxlDynamicCapacityExtentList *records,
+                                uint8_t rid);
+    bool (*mhd_reclaim_extents)(PCIDevice *d,
+                                CXLDCExtentGroupList *groups,
+                                CXLUpdateDCExtentListInPl *in);
+    bool (*mhd_release_extent)(PCIDevice *d, uint64_t dpa, uint64_t len);
 };
 
 struct CSWMBCCIDev {
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH RFC v3 2/3] cxl_type3: add MHD callbacks

Quoted 15 lines · On Fri, 18 Oct 2024 12:12:51 -0400 Gregory Price <[email protected]> wrote:

From: Svetly Todorov <[email protected]>

Introduce an API for validating DC adds, removes, and responses against a multi-headed device.

mhd_reserve_extents() is called during a DC add request. This allows a multi-headed device to check whether the requested extents belong to another host. If not, then this function can claim those extents in the MHD state and allow the cxl_type3 code to follow suit in the host-local blk_bitmap.

mhd_reclaim_extents() is called during the DC add response. It allows the MHD to reclaim extents that were preallocated to a host during the request but rejected in the response.

Quoted 6 lines

mhd_release_extent() is called during the DC release response. It can be invoked after a host frees an extent in its local bitmap, allowing the MHD handler to release that same extent in the multi-host state.

Hi Gregory, Svetly,

A few minor comments inline. I want to think a bit more on the general approach before providing a broader reply.

If I apply this on my cxl staging tree an can easily tidy the stuff up I mention whilst doing so.

Thanks,

Jonathan

Quoted 19 lines
---
 hw/cxl/cxl-mailbox-utils.c  | 28 +++++++++++++++++++++++++++-
 hw/mem/cxl_type3.c          | 17 +++++++++++++++++
 include/hw/cxl/cxl_device.h |  8 ++++++++
 3 files changed, 52 insertions(+), 1 deletion(-)
hw/cxl/cxl-mailbox-utils.c +1 -0
index 10de26605c..112272e9ac 100644
@@ -2545,6 +2545,7 @@ static CXLRetCode cmd_dcd_add_dyn_cap_rsp(const struct cxl_cmd *cmd,
 {
     CXLUpdateDCExtentListInPl *in = (void *)payload_in;
     CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
+    CXLType3Class *cvc = CXL_TYPE3_GET_CLASS(ct3d);
     CXLDCExtentList *extent_list = &ct3d->dc.extents;
     uint32_t i;
     uint64_t dpa, len;

hw/cxl/cxl-mailbox-utils.c static CXLRetCode cmd_dcd_add_dyn_cap_rsp(const struct cxl_cmd *cmd, · line 2587

Quoted 8 lines
hw/cxl/cxl-mailbox-utils.c +5 -0
@@ -2579,6 +2580,11 @@ static CXLRetCode cmd_dcd_add_dyn_cap_rsp(const struct cxl_cmd *cmd,
         ct3d->dc.total_extent_count += 1;
         ct3_set_region_block_backed(ct3d, dpa, len);
     }
+
+    if (cvc->mhd_reclaim_extents)
+        cvc->mhd_reclaim_extents(&ct3d->parent_obj, &ct3d->dc.extents_pending,
+                in);
+

Trivial but qemu style requires {} always.

I'd also indent that in to be aligned under the &

Quoted 39 lines
hw/cxl/cxl-mailbox-utils.c +11 -1
     /* Remove the first extent group in the pending list */
     cxl_extent_group_list_delete_front(&ct3d->dc.extents_pending);
 
@@ -2612,6 +2618,7 @@ static CXLRetCode cxl_dc_extent_release_dry_run(CXLType3Dev *ct3d,
         uint32_t *updated_list_size)
 {
     CXLDCExtent *ent, *ent_next;
+    CXLType3Class *cvc = CXL_TYPE3_GET_CLASS(ct3d);
     uint64_t dpa, len;
     uint32_t i;
     int cnt_delta = 0;
@@ -2632,6 +2639,13 @@ static CXLRetCode cxl_dc_extent_release_dry_run(CXLType3Dev *ct3d,
             goto free_and_exit;
         }
 
+        /* In an MHD, check that this DPA range belongs to this host */
+        if (cvc->mhd_access_valid &&
+            !cvc->mhd_access_valid(&ct3d->parent_obj, dpa, len)) {
+            ret = CXL_MBOX_INVALID_PA;
+            goto free_and_exit;
+        }
+
         /* After this point, extent overflow is the only error can happen */
         while (len > 0) {
             QTAILQ_FOREACH(ent, updated_list, node) {
@@ -2704,9 +2718,11 @@ static CXLRetCode cmd_dcd_release_dyn_cap(const struct cxl_cmd *cmd,
 {
     CXLUpdateDCExtentListInPl *in = (void *)payload_in;
     CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
+    CXLType3Class *cvc = CXL_TYPE3_GET_CLASS(ct3d);
     CXLDCExtentList updated_list;
     CXLDCExtent *ent, *ent_next;
-    uint32_t updated_list_size;
+    uint32_t updated_list_size, i;
+    uint64_t dpa, len;
     CXLRetCode ret;
 
     if (in->num_entries_updated == 0) {

hw/cxl/cxl-mailbox-utils.c static CXLRetCode cmd_dcd_release_dyn_cap(const struct cxl_cmd *cmd, · line 2744

Quoted 5 lines
hw/cxl/cxl-mailbox-utils.c +2 -0
@@ -2724,6 +2740,16 @@ static CXLRetCode cmd_dcd_release_dyn_cap(const struct cxl_cmd *cmd,
         return ret;
     }
 
+    /* Updated_entries contains the released extents. Free those in the MHD */
+    for (i = 0; cvc->mhd_release_extent && i < in->num_entries_updated; ++i) {

local style is post increment.

Also, indent isn't too bad if you pull the mhd_release_extent out of the loop condition as an if statement. It think that ends up more reaable.

hw/cxl/cxl-mailbox-utils.c static CXLRetCode cmd_dcd_release_dyn_cap(const struct cxl_cmd *cmd, · line 2748

Quoted 4 lines
hw/cxl/cxl-mailbox-utils.c +4 -0
+        dpa = in->updated_entries[i].start_dpa;
+        len = in->updated_entries[i].len;
+
+        if (cvc->mhd_release_extent) {

Checked in loop condition as well so this isn't needed.

Quoted 20 lines
hw/cxl/cxl-mailbox-utils.c +4 -0
+            cvc->mhd_release_extent(&ct3d->parent_obj, dpa, len);
+        }
+    }
+
     /*
      * If the dry run release passes, the returned updated_list will
      * be the updated extent list and we just need to clear the extents
hw/mem/cxl_type3.c +1 -0
index b7b24b6a32..a94b9931d2 100644
@@ -799,6 +799,7 @@ static void cxl_destroy_dc_regions(CXLType3Dev *ct3d)
 {
     CXLDCExtent *ent, *ent_next;
     CXLDCExtentGroup *group, *group_next;
+    CXLType3Class *cvc = CXL_TYPE3_CLASS(ct3d);
     int i;
     CXLDCRegion *region;
 

hw/mem/cxl_type3.c static void cxl_destroy_dc_regions(CXLType3Dev *ct3d) · line 823

Quoted 6 lines
hw/mem/cxl_type3.c +3 -0
@@ -817,6 +818,10 @@ static void cxl_destroy_dc_regions(CXLType3Dev *ct3d)
     for (i = 0; i < ct3d->dc.num_regions; i++) {
         region = &ct3d->dc.regions[i];
         g_free(region->blk_bitmap);
+        if (cvc->mhd_release_extent) {
+            cvc->mhd_release_extent(&ct3d->parent_obj, region->base,
+                    region->len);

Indent to align after (

Quoted 21 lines
hw/mem/cxl_type3.c +3 -0
+        }
     }
 }
 
@@ -2077,6 +2082,7 @@ static void qmp_cxl_process_dynamic_capacity_prescriptive(const char *path,
     CXLEventDynamicCapacity dCap = {};
     CXLEventRecordHdr *hdr = &dCap.hdr;
     CXLType3Dev *dcd;
+    CXLType3Class *cvc;
     uint8_t flags = 1 << CXL_EVENT_TYPE_INFO;
     uint32_t num_extents = 0;
     CxlDynamicCapacityExtentList *list;
@@ -2094,6 +2100,7 @@ static void qmp_cxl_process_dynamic_capacity_prescriptive(const char *path,
     }
 
     dcd = CXL_TYPE3(obj);
+    cvc = CXL_TYPE3_GET_CLASS(dcd);
     if (!dcd->dc.num_regions) {
         error_setg(errp, "No dynamic capacity support from the device");
         return;

hw/mem/cxl_type3.c static void qmp_cxl_process_dynamic_capacity_prescriptive(const char *path, · line 2179

Quoted 7 lines
hw/mem/cxl_type3.c +4 -0
@@ -2166,6 +2173,13 @@ static void qmp_cxl_process_dynamic_capacity_prescriptive(const char *path,
         num_extents++;
     }
 
+    /* If this is an MHD, attempt to reserve the extents */
+    if (type == DC_EVENT_ADD_CAPACITY && cvc->mhd_reserve_extents &&
+       !cvc->mhd_reserve_extents(&dcd->parent_obj, records, rid)) {
+        error_setg(errp, "mhsld is enabled and extent reservation failed");

I'd reorganize this so you can get the return value. It might be useful in the long run.

Quoted 42 lines
hw/mem/cxl_type3.c +6 -0
+        return;
+    }
+
     /* Create extent list for event being passed to host */
     i = 0;
     list = records;
@@ -2304,6 +2318,9 @@ static void ct3_class_init(ObjectClass *oc, void *data)
     cvc->set_cacheline = set_cacheline;
     cvc->mhd_get_info = NULL;
     cvc->mhd_access_valid = NULL;
+    cvc->mhd_reserve_extents = NULL;
+    cvc->mhd_reclaim_extents = NULL;
+    cvc->mhd_release_extent = NULL;
 }
 
 static const TypeInfo ct3d_info = {
include/hw/cxl/cxl_device.h +8 -0
index b2dc7fb769..13c97b576f 100644
@@ -14,6 +14,7 @@
 #include "hw/pci/pci_device.h"
 #include "hw/register.h"
 #include "hw/cxl/cxl_events.h"
+#include "qapi/qapi-commands-cxl.h"
 
 #include "hw/cxl/cxl_cpmu.h"
 /*
@@ -682,6 +683,13 @@ struct CXLType3Class {
                                size_t *len_out,
                                CXLCCI *cci);
     bool (*mhd_access_valid)(PCIDevice *d, uint64_t addr, unsigned int size);
+    bool (*mhd_reserve_extents)(PCIDevice *d,
+                                CxlDynamicCapacityExtentList *records,
+                                uint8_t rid);
+    bool (*mhd_reclaim_extents)(PCIDevice *d,
+                                CXLDCExtentGroupList *groups,
+                                CXLUpdateDCExtentListInPl *in);
+    bool (*mhd_release_extent)(PCIDevice *d, uint64_t dpa, uint64_t len);
 };
 
 struct CSWMBCCIDev {
#4 lore raw

RFC PATCH v3 3/3 mhsld: implement MHSLD device

In reply to Gregory Price

From: Svetly Todorov <[email protected]>

Using a shared-memory bytemap, validates that DC adds, releases, and reclamations happen on extents belonging to the appropriate host.

The MHSLD device inherits from the CXL_TYPE3 class and adds the following configuration options:
--mhd-head=<u32>
--mhd-state_file=<str>
--mhd-init=<bool>

--mhd-head specifies the head ID of the host on the given device.

--mhd-state_file is the name of the shared-memory-backed file used to store the MHD state.

--mhd-init indicates whether this QEMU instance should initialize the state_file; if so, the instance will create the file if it does not exist, ftruncate it to the appropriate size, and initialize its header. It is assumed that the --mhd-init instance is run and allowed to completely finish configuration before any other guests access the shared state.

The shared state file only needs to be intialized once. Even if a guest dies without clearing the ownership bits associated with its head-ID, future guests with that ID will clear those bits in cxl_mhsld_realize(), regardless of whether mhd_init is true or false.

The following command line options create an MHSLD with 4GB of backing memory, whose state is tracked in /dev/shm/mhd_metadata. --mhd-init=true tells this instance to initialize the state as described above.

./qemu-system_x86-64 \
[... other options ...] \
-device pxb-cxl,id=cxl.0,bus=pcie.0,bus_nr=52 \
-device cxl-rp,id=rp0,bus=cxl.0,chassis=0,port=0,slot=0 \
-object memory-backend-ram,id=mem0,size=4G \
-device cxl-mhsld,bus=rp0,num-dc-regions=1,volatile-dc-memdev=mem0,id=cxl-mem0,sn=66667,mhd-head=0,mhd-state_file=mhd_metadata,mhd-init=true \
-M cxl-fmw.0.targets.0=cxl.0,cxl-fmw.0.size=4G \
-qmp unix:/tmp/qmp-sock-1,server,nowait

Once this guest completes setup, other guests looking to access the device can be booted with the same configuration options, but with --mhd-head != 0,
--mhd-init=false,
and a different QMP socket.

---
 hw/cxl/Kconfig           |   1 +
 hw/cxl/meson.build       |   1 +
 hw/cxl/mhsld/Kconfig     |   4 +
 hw/cxl/mhsld/meson.build |   3 +
 hw/cxl/mhsld/mhsld.c     | 456 +++++++++++++++++++++++++++++++++++++++
 hw/cxl/mhsld/mhsld.h     |  75 +++++++
 6 files changed, 540 insertions(+)
 create mode 100644 hw/cxl/mhsld/Kconfig
 create mode 100644 hw/cxl/mhsld/meson.build
 create mode 100644 hw/cxl/mhsld/mhsld.c
 create mode 100644 hw/cxl/mhsld/mhsld.h
hw/cxl/Kconfig +1 -0
index e603839a62..919e59b598 100644
@@ -1,3 +1,4 @@
+source mhsld/Kconfig
 source vendor/Kconfig
 
 config CXL
hw/cxl/meson.build +1 -0
index e8c8c1355a..394750dd19 100644
@@ -16,4 +16,5 @@ system_ss.add(when: 'CONFIG_I2C_MCTP_CXL', if_true: files('i2c_mctp_cxl.c'))
 
 system_ss.add(when: 'CONFIG_ALL', if_true: files('cxl-host-stubs.c'))
 
+subdir('mhsld')
 subdir('vendor')
hw/cxl/mhsld/Kconfig new file +4 -0
new file mode 100644
index 0000000000..dc2be15140
@@ -0,0 +1,4 @@
+config CXL_MHSLD
+    bool
+    depends on CXL_MEM_DEVICE
+    default y
hw/cxl/mhsld/meson.build new file +3 -0
new file mode 100644
index 0000000000..c595558f8a
@@ -0,0 +1,3 @@
+if host_os == 'linux'
+	system_ss.add(when: 'CONFIG_CXL_MHSLD', if_true: files('mhsld.c',))
+endif
hw/cxl/mhsld/mhsld.c new file +456 -0

456 lines not on this page · raw message

hw/cxl/mhsld/mhsld.h new file +75 -0

75 lines not on this page · raw message

Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH RFC v3 3/3] mhsld: implement MHSLD device

Quoted 8 lines · On Fri, 18 Oct 2024 12:12:52 -0400 Gregory Price <[email protected]> wrote:

From: Svetly Todorov <[email protected]>

Using a shared-memory bytemap, validates that DC adds, releases, and reclamations happen on extents belonging to the appropriate host.

The MHSLD device inherits from the CXL_TYPE3 class and adds the following configuration options:

As in previous the -- syntax is somewhat confusing as that's not how the parameters are applied.

Quoted 16 lines

--mhd-head=<u32>
--mhd-state_file=<str>
--mhd-init=<bool>

--mhd-head specifies the head ID of the host on the given device.

--mhd-state_file is the name of the shared-memory-backed file used to store the MHD state.

--mhd-init indicates whether this QEMU instance should initialize the state_file; if so, the instance will create the file if it does not exist, ftruncate it to the appropriate size, and initialize its header. It is assumed that the --mhd-init instance is run and allowed to completely finish configuration before any other guests access the shared state.

Quoted 4 lines

The shared state file only needs to be intialized once. Even if a guest dies without clearing the ownership bits associated with its head-ID, future guests with that ID will clear those bits in cxl_mhsld_realize(), regardless of whether mhd_init is true or false.

That sounds like a race condition if not all hosts are brought up before the first add.

Quoted 15 lines

The following command line options create an MHSLD with 4GB of backing memory, whose state is tracked in /dev/shm/mhd_metadata. --mhd-init=true tells this instance to initialize the state as described above.

./qemu-system_x86-64 \
[... other options ...] \

(quoted diff) +0 -6
-device pxb-cxl,id=cxl.0,bus=pcie.0,bus_nr=52 \
-device cxl-rp,id=rp0,bus=cxl.0,chassis=0,port=0,slot=0 \
-object memory-backend-ram,id=mem0,size=4G \
-device cxl-mhsld,bus=rp0,num-dc-regions=1,volatile-dc-memdev=mem0,id=cxl-mem0,sn=66667,mhd-head=0,mhd-state_file=mhd_metadata,mhd-init=true \
-M cxl-fmw.0.targets.0=cxl.0,cxl-fmw.0.size=4G \
-qmp unix:/tmp/qmp-sock-1,server,nowait
Quoted 8 lines

Once this guest completes setup, other guests looking to access the device can be booted with the same configuration options, but with --mhd-head != 0,
--mhd-init=false,
and a different QMP socket.

A few trivial things inline.

In general the scheme looks workable but I'm not sure the contraints at setup time etc are suitable for an upstream solution. Certainly a useful tool to have for kernel development though so I'll try and find time in next few days to apply this on my gitlab tree.

Longer term I think we need a more complex external program or a main / proxy type arrangement so that ordering requirements can be enforce and we can have richer info. Having to chat to each qmp interface independently works fine is also a bit more complex than I think we would eventually want.

Having a solution in place though will make it much easier to move towards an eventual upstreamable solution. This is a great place to start from!

Jonathan

hw/cxl/mhsld/mhsld.c line 304

Quoted 4 lines
hw/cxl/mhsld/mhsld.c new file +4 -0
+static bool cxl_mhsld_release_extent(PCIDevice *d, uint64_t dpa, uint64_t len)
+{
+    cxl_mhsld_state_clear(CXL_MHSLD(d), dpa / MHSLD_BLOCK_SZ,
+        len / MHSLD_BLOCK_SZ);

Trivial but align after (

Quoted 22 lines
(quoted diff) +2 -0
+    return true;
+}
hw/cxl/mhsld/mhsld.h new file +12 -0
new file mode 100644
index 0000000000..e7ead1f0d2
@@ -0,0 +1,75 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * Copyright (c) 2024 MemVerge Inc.
+ *
+ */
+
+#ifndef CXL_MHSLD_H
+#define CXL_MHSLD_H
+#include <stdint.h>
+#include "hw/cxl/cxl.h"
+#include "hw/cxl/cxl_mailbox.h"

hw/cxl/mhsld/mhsld.h line 22

Quoted 10 lines
hw/cxl/mhsld/mhsld.h new file +10 -0
+#include "hw/cxl/cxl_device.h"
+#include "qemu/units.h"
+
+#define MHSLD_BLOCK_SZ (2 * MiB)
+
+/*
+ * We limit the number of heads to prevent the shared state
+ * region from becoming a major memory hog.  We need 512MB of
+ * memory space to track 8-host ownership of 4GB of memory in
+ * blocks of 2MB.  This can change if the block size is increased.

I'm lost what makes up that size?

Quoted 53 lines
hw/cxl/mhsld/mhsld.h new file +53 -0
+ */
+#define MHSLD_HEADS  (8)
+
+/*
+ * The shared state cannot have 2 variable sized regions
+ * so we have to max out the ldmap.
+ */
+typedef struct MHSLDSharedState {
+    uint8_t nr_heads;
+    uint8_t nr_lds;
+    uint8_t ldmap[MHSLD_HEADS];
+    uint64_t nr_blocks;
+    uint8_t blocks[];
+} MHSLDSharedState;
+
+struct CXLMHSLDState {
+    CXLType3Dev ct3d;
+    bool mhd_init;
+    char *mhd_state_file;
+    int mhd_state_fd;
+    size_t mhd_state_size;
+    uint32_t mhd_head;
+    MHSLDSharedState *mhd_state;
+};
+
+struct CXLMHSLDClass {
+    CXLType3Class parent_class;
+};
+
+enum {
+    MHSLD_MHD = 0x55,
+        #define GET_MHD_INFO 0x0
+};
+
+/*
+ * MHD Get Info Command
+ * Returns information the LD's associated with this head
+ */
+typedef struct MHDGetInfoInput {
+    uint8_t start_ld;
+    uint8_t ldmap_len;
+} QEMU_PACKED MHDGetInfoInput;
+
+typedef struct MHDGetInfoOutput {
+    uint8_t nr_lds;
+    uint8_t nr_heads;
+    uint16_t resv1;
+    uint8_t start_ld;
+    uint8_t ldmap_len;
+    uint16_t resv2;
+    uint8_t ldmap[];
+} QEMU_PACKED MHDGetInfoOutput;
+#endif
#6 lore raw

Re: [PATCH RFC v3 3/3] mhsld: implement MHSLD device

Quoted 5 lines · Jonathan Cameron via
Quoted 2 lines · On Fri, 18 Oct 2024 12:12:52 -0400 Gregory Price <[email protected]> wrote:

From: Svetly Todorov <[email protected]>

Quoted 8 lines
Quoted 4 lines

The shared state file only needs to be intialized once. Even if a guest dies without clearing the ownership bits associated with its head-ID, future guests with that ID will clear those bits in cxl_mhsld_realize(), regardless of whether mhd_init is true or false.

That sounds like a race condition if not all hosts are brought up before the first add.

We weighed this against having to do an external setup like

# SHMID = ./create_sharedmem.sh

# ./launch_qemu --shmid=$SHMID

Which is what the original non-generalized prototype did.

So yeah, there's a race condition AND a footgun (setting init AFTER qemu instances are already using the memory will blow the state away).

This was intended.

As you allude to in the next chunk - the only real way to get around this is to have an entirely external process serialize access.

Quoted 26 lines
Quoted 15 lines

The following command line options create an MHSLD with 4GB of backing memory, whose state is tracked in /dev/shm/mhd_metadata. --mhd-init=true tells this instance to initialize the state as described above.

./qemu-system_x86-64 \
[... other options ...] \

(quoted diff) +0 -6
-device pxb-cxl,id=cxl.0,bus=pcie.0,bus_nr=52 \
-device cxl-rp,id=rp0,bus=cxl.0,chassis=0,port=0,slot=0 \
-object memory-backend-ram,id=mem0,size=4G \
-device cxl-mhsld,bus=rp0,num-dc-regions=1,volatile-dc-memdev=mem0,id=cxl-mem0,sn=66667,mhd-head=0,mhd-state_file=mhd_metadata,mhd-init=true \
-M cxl-fmw.0.targets.0=cxl.0,cxl-fmw.0.size=4G \
-qmp unix:/tmp/qmp-sock-1,server,nowait
Quoted 8 lines

Once this guest completes setup, other guests looking to access the device can be booted with the same configuration options, but with --mhd-head != 0,
--mhd-init=false,
and a different QMP socket.

A few trivial things inline.

Quoted 7 lines

In general the scheme looks workable but I'm not sure the contraints at setup time etc are suitable for an upstream solution. Certainly a useful tool to have for kernel development though so I'll try and find time in next few days to apply this on my gitlab tree.

Longer term I think we need a more complex external program or a main / proxy type arrangement so that ordering requirements can be enforce

I marginally disagree. We have to check ownership during memory use.

We should try not to have an external process dependency for deferencing a pointer backed by an emulated DCD device. The current overhead is bad enough.

The shared memory use here mostly limits that overhead to cache invalidation, and keeps the entire system fairly simple.

All this is to say - we err'd on the side of keeping it simple, even if it has a few stupid footguns. Obviously open to ideas, though.

Quoted 4 lines

and we can have
richer info. Having to chat to each qmp interface independently works fine is also a bit more complex than I think we would eventually want.

This is a small component in someone's fabric manager that translates their requests into QMP commands. Whatever we ultimately decide on, the complexity here is about the same.

Quoted 5 lines

Having a solution in place though will make it much easier to move towards an eventual upstreamable solution. This is a great place to start from!

Jonathan

Quoted 8 lines
Quoted 5 lines

+/*
+ * We limit the number of heads to prevent the shared state
+ * region from becoming a major memory hog. We need 512MB of
+ * memory space to track 8-host ownership of 4GB of memory in
+ * blocks of 2MB. This can change if the block size is increased.

I'm lost what makes up that size?

I think the math is wrong here, we may have calculated based on a larger capacity. I need to go back and look at how we came to 512MB.

~Gregory

#7 lore raw

Re: [PATCH RFC v3 3/3] mhsld: implement MHSLD device

Quoted 66 lines · On Thu, 12 Dec 2024 14:52:02 -0500 Gregory Price <[email protected]> wrote:
Quoted 5 lines · Jonathan Cameron via
Quoted 2 lines · On Fri, 18 Oct 2024 12:12:52 -0400 Gregory Price <[email protected]> wrote:

From: Svetly Todorov <[email protected]>

Quoted 8 lines
Quoted 4 lines

The shared state file only needs to be intialized once. Even if a guest dies without clearing the ownership bits associated with its head-ID, future guests with that ID will clear those bits in cxl_mhsld_realize(), regardless of whether mhd_init is true or false.

That sounds like a race condition if not all hosts are brought up before the first add.

We weighed this against having to do an external setup like

# SHMID = ./create_sharedmem.sh

# ./launch_qemu --shmid=$SHMID

Which is what the original non-generalized prototype did.

So yeah, there's a race condition AND a footgun (setting init AFTER qemu instances are already using the memory will blow the state away).

This was intended.

As you allude to in the next chunk - the only real way to get around this is to have an entirely external process serialize access.

Quoted 26 lines
Quoted 15 lines

The following command line options create an MHSLD with 4GB of backing memory, whose state is tracked in /dev/shm/mhd_metadata. --mhd-init=true tells this instance to initialize the state as described above.

./qemu-system_x86-64 \
[... other options ...] \

(quoted diff) +0 -6
-device pxb-cxl,id=cxl.0,bus=pcie.0,bus_nr=52 \
-device cxl-rp,id=rp0,bus=cxl.0,chassis=0,port=0,slot=0 \
-object memory-backend-ram,id=mem0,size=4G \
-device cxl-mhsld,bus=rp0,num-dc-regions=1,volatile-dc-memdev=mem0,id=cxl-mem0,sn=66667,mhd-head=0,mhd-state_file=mhd_metadata,mhd-init=true \
-M cxl-fmw.0.targets.0=cxl.0,cxl-fmw.0.size=4G \
-qmp unix:/tmp/qmp-sock-1,server,nowait
Quoted 8 lines

Once this guest completes setup, other guests looking to access the device can be booted with the same configuration options, but with --mhd-head != 0,
--mhd-init=false,
and a different QMP socket.

A few trivial things inline.

Quoted 7 lines

In general the scheme looks workable but I'm not sure the contraints at setup time etc are suitable for an upstream solution. Certainly a useful tool to have for kernel development though so I'll try and find time in next few days to apply this on my gitlab tree.

Longer term I think we need a more complex external program or a main / proxy type arrangement so that ordering requirements can be enforce

I marginally disagree. We have to check ownership during memory use.

Quoted 8 lines

We should try not to have an external process dependency for deferencing a pointer backed by an emulated DCD device. The current overhead is bad enough.

The shared memory use here mostly limits that overhead to cache invalidation, and keeps the entire system fairly simple.

All this is to say - we err'd on the side of keeping it simple, even if it has a few stupid footguns. Obviously open to ideas, though.

Need to mess around with it, but I don't in general think each host needs to see the global protection map - they just need to know what they can access.

I don't think anyone is going to do 2MiB granularity on a real device (or at least not for bulk capacity) so the necessary data may not be that large. Obviously the cost of updating it will be higher, but meh, that's not that common an operation.

Quoted 9 lines
Quoted 5 lines

and we can have
richer info. Having to chat to each qmp interface independently works fine is also a bit more complex than I think we would eventually want.

This is a small component in someone's fabric manager that translates their requests into QMP commands. Whatever we ultimately decide on, the complexity here is about the same.

It's the qmp commands to a bunch of different qemu instances pretending to be one Multi Head device.
Sure we can insert a proxy to act as that one, but then we more or less have an external process mediating that.

Quoted 12 lines
Quoted 12 lines

Having a solution in place though will make it much easier to move towards an eventual upstreamable solution. This is a great place to start from!

Jonathan

Quoted 5 lines

+/*
+ * We limit the number of heads to prevent the shared state
+ * region from becoming a major memory hog. We need 512MB of
+ * memory space to track 8-host ownership of 4GB of memory in
+ * blocks of 2MB. This can change if the block size is increased.

Quoted 5 lines
Quoted 2 lines

I'm lost what makes up that size?

I think the math is wrong here, we may have calculated based on a larger capacity. I need to go back and look at how we came to 512MB.

4TB maybe?

Jonathan

Quoted 2 lines

~Gregory

#8 lore raw

Re: [PATCH RFC v3 3/3] mhsld: implement MHSLD device

Quoted 14 lines · Jonathan Cameron

On Thu, 12 Dec 2024 14:52:02 -0500
Gregory Price <[email protected]> wrote:

I don't think anyone is going to do 2MiB granularity on a real device (or at least not for bulk capacity) so the necessary data may not be that large. Obviously the cost of updating it will be higher, but meh, that's not that common an operation.

Quoted 6 lines
Quoted 5 lines

and we can have
richer info. Having to chat to each qmp interface independently works fine is also a bit more complex than I think we would eventually want.

Quoted 9 lines
Quoted 3 lines

This is a small component in someone's fabric manager that translates their requests into QMP commands. Whatever we ultimately decide on, the complexity here is about the same.

It's the qmp commands to a bunch of different qemu instances pretending to be one Multi Head device.
Sure we can insert a proxy to act as that one, but then we more or less have an external process mediating that.

I'm not against changing this, I just think it's going to increase complexity for little value.

I personally like the idea of keeping it the way it is and slapping a small proxy on top that emulates the "out of band" interface to the device - but all it does behind the scenes is send out a QMP command (that already exists) to a guest that registered itself with the proxy.

This separates the complexity of implementing FMAPI functionality and the complexity of how an individual instance of QEMU go about handling add/access/remove semantics for DCD events.

The annoying thought experiment here is when someone wants their in-band host interface to have access to the FMAPI

great now i've made myself grumpy

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

Having a solution in place though will make it much easier to move towards an eventual upstreamable solution. This is a great place to start from!

Jonathan

Quoted 5 lines

+/*
+ * We limit the number of heads to prevent the shared state
+ * region from becoming a major memory hog. We need 512MB of
+ * memory space to track 8-host ownership of 4GB of memory in
+ * blocks of 2MB. This can change if the block size is increased.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I'm lost what makes up that size?

I think the math is wrong here, we may have calculated based on a larger capacity. I need to go back and look at how we came to 512MB.

4TB maybe?

Jonathan

Yeah that's probably what it was. I think that's what I was basing it off of at the time.

~Gregory

#9 lore raw

Re: [PATCH RFC v3 3/3] mhsld: implement MHSLD device

In reply to Gregory Price
Quoted 6 lines · On Fri, 18 Oct 2024 12:12:52 -0400 Gregory Price <[email protected]> wrote:

From: Svetly Todorov <[email protected]>

Using a shared-memory bytemap, validates that DC adds, releases, and reclamations happen on extents belonging to the appropriate host.

I'll carry this on my staging tree for now gitlab.com/jic23/qemu (latest date branch). I'll push out a bit later today.
Few rebase related changes were needed. I've not tested it yet.

Also I have dropped the previous prototype device Niagara on basis this should give people a better platform to work with.

Jonathan

#10 lore raw

Re: [PATCH RFC v3 3/3] mhsld: implement MHSLD device

Quoted 9 lines · Jonathan Cameron
Quoted 6 lines · On Fri, 18 Oct 2024 12:12:52 -0400 Gregory Price <[email protected]> wrote:

From: Svetly Todorov <[email protected]>

Using a shared-memory bytemap, validates that DC adds, releases, and reclamations happen on extents belonging to the appropriate host.

Quoted 7 lines

I'll carry this on my staging tree for now gitlab.com/jic23/qemu (latest date branch). I'll push out a bit later today.
Few rebase related changes were needed. I've not tested it yet.

Also I have dropped the previous prototype device Niagara on basis this should give people a better platform to work with.

Agreed. Niagara was a good step to get here.

Let me know how I can help.

~Gregory