Re: [PATCH v1 04/14] cxl/mem: Introduce DC event handlers
From: Anisa Su
Date: Thu Dec 03 2026 - 22:48:14 EST
On Tue, Nov 17, 2026 at 02:54:27AM +0000, Jonathan Cameron wrote:
> On Thu, 5 Nov 2026 08:02:28 +0900
> Anisa Su <anisa.su887@xxxxxxxxx> wrote:
>
> > From: Ira Weiny <iweiny@xxxxxxxxxx>
> >
> > Add the handling skeleton for DC Add and DC Release events. Per CXL
> > r4.0 Section 8.2.10.9.9.3, the host is expected to respond to one or
> > more Add Capacity Event Record(s) with an Add Dynamic Capacity Response
> > (Opcode 4802h) to acknowledge which extents the host accepts for use.
> > Similarly, per CXL r4.0 Section 8.2.10.9.9.4, the host is expected to
> > respond to Release Capacity Event Record(s) with a Release Dynamic
> > Capacity command (Opcode 4803h), or to issue one that is not
> > associated with any event to release capacity on its own.
> >
> > The records of one operation can be chained by the More flag. Chained
> > records are contiguous in the event log and share one event type (CXL
> > r4.0 Table 8-229), so a single staging context, mds->dc_chain, holds
> > the records of the open chain until the closing record (More=0)
> > arrives and the chain is processed and responded to as a whole. A
> > record that fails to stage, or one of the other type arriving inside an
> > open chain, drops the chain: an add is refused with an empty response,
> > a release is left for the device to retry.
> >
> > This commit refuses all extents from Add Capacity chains and acks all
> > Release Capacity chains. Subsequent commits implement accepting and
> > releasing extents.
> >
> > Forced Capacity Release and Region Configuration Updated events are
> > logged and ignored.
> >
> > Based on an original patch by Navneet Singh.
> >
> > Signed-off-by: Ira Weiny <iweiny@xxxxxxxxxx>
> > Co-developed-by: Anisa Su <anisa.su@xxxxxxxxxxx>
> > Signed-off-by: Anisa Su <anisa.su@xxxxxxxxxxx>
> > Tested-by: Wonjae Lee <wj28.lee@xxxxxxxxxxx>
> > Tested-by: Junhee Park <jh9934.park@xxxxxxxxxxx>
> > Tested-by: Heesoo Kim <habil.kim@xxxxxxxxxxx>
> Sashiko does raise an interesting question about trailing events
> in a chain after an error. If it's shared capacity we aren't allowed
> a partial accept. Otherwise it is fine (I think) but perhaps not complexity we
> want. So probably need some flag to say problem occured on a record
> with more flag set. Keep dropping until we have had a closing record.
>
Yes, for simplicity, v2 will just abort the regardless of if the
capacity is shared or not. Also IIUC it's really tag groups that must be
all or none, and non-shareable capacity can be optionally tagged.
So as you suggested, a new flag is aded:
+ * @discard: a record failed to stage; refuse the chain when it closes
...
struct cxl_dc_event_chain {
struct list_head pending_extents;
u8 event_type;
+ bool discard;
};
Set chain->discard to true if staging fails in handle_dcd_chain_event():
if (chain->discard ||
stage_pending_extent(&chain->pending_extents, &event->extent))
chain->discard = true;
> For force release, I'm not sure if we should just panic() (maybe
> with a sanity check that the range was mapped) or ignore it and
> die in a storm of poison (hopefully rather than zeros).
>
Hmm, will do the panic() for v2. The check to see if the capacity is
being used belongs to the DAX half, so this half just gets unconditional
panic(), which should be fine since DCD is turned off.
> > ---
> > drivers/cxl/core/mbox.c | 246 ++++++++++++++++++++++++++++++++++++++-
> > drivers/cxl/core/trace.h | 3 +-
> > drivers/cxl/cxl.h | 9 ++
> > drivers/cxl/cxlmem.h | 46 ++++++++
> > drivers/cxl/pci.c | 20 +++-
> > include/cxl/event.h | 37 ++++++
> > 6 files changed, 353 insertions(+), 8 deletions(-)
> >
> > diff --git a/drivers/cxl/core/mbox.c b/drivers/cxl/core/mbox.c
> > index 13a68f5cef69..3acf5ace48db 100644
> > --- a/drivers/cxl/core/mbox.c
> > +++ b/drivers/cxl/core/mbox.c
>
> > +static int cxl_send_dc_response(struct cxl_memdev_state *mds, int opcode,
> > + struct list_head *extent_list, int cnt)
> > +{
> > + struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox;
> > + struct cxl_mbox_dc_response *p;
> > + struct cxl_extent_list_node *pos, *tmp;
> > + struct cxl_extent *extent;
> > + u32 pl_index;
> > +
> > + size_t pl_size = struct_size(p, extent_list, cnt);
> > + u32 max_extents = cnt;
> > +
> > + /* May have to use more bit on response. */
> > + if (pl_size > cxl_mbox->payload_size) {
> > + max_extents = (cxl_mbox->payload_size - sizeof(*p)) /
> > + sizeof(struct updated_extent_list);
> > + pl_size = struct_size(p, extent_list, max_extents);
> > + }
> > +
> > + struct cxl_mbox_dc_response *response __free(kfree) =
> > + kzalloc(pl_size, GFP_KERNEL);
> > + if (!response) {
> > + struct cxl_mbox_dc_response empty = { };
> > +
> > + /* An empty Release DC is invalid (CXL r4.0 Table 8-352) */
> > + if (opcode == CXL_MBOX_OP_RELEASE_DC)
> > + return -ENOMEM;
> > + /* Refuse all extents rather than stall the device. */
> > + return send_one_response(cxl_mbox, &empty, opcode, 0, 0);
> > + }
> > +
> > + /* Set __counted_by to capacity before populating; reset per response. */
> > + response->extent_list_size = cpu_to_le32(max_extents);
> > +
> > + if (cnt == 0)
> > + return send_one_response(cxl_mbox, response, opcode, 0, 0);
> > +
> > + pl_index = 0;
> > + list_for_each_entry_safe(pos, tmp, extent_list, list) {
> > + extent = pos->extent;
> > + response->extent_list[pl_index].dpa_start = extent->start_dpa;
> > + response->extent_list[pl_index].length = extent->length;
> > + pl_index++;
> > +
> > + if (pl_index == max_extents) {
> Maybe flip?
> if (pl_index != max_extents)
> continue;
>
> flags = 0;
>
> ...
Flipped
>
> > + u8 flags = 0;
> > + int rc;
> > +
> > + if (pl_index < cnt)
> > + flags |= CXL_DCD_EVENT_MORE;
> > + rc = send_one_response(cxl_mbox, response, opcode,
> > + pl_index, flags);
> > + if (rc)
> > + return rc;
> > + cnt -= pl_index;
> > + if (cnt < max_extents)
> > + max_extents = cnt;
> > + pl_index = 0;
> > + }
> > + }
> > +
> > + if (!pl_index) /* nothing more to do */
> > + return 0;
> > + return send_one_response(cxl_mbox, response, opcode, pl_index, 0);
> > +}
>
> > +
> > +static int stage_pending_extent(struct list_head *pending,
> > + struct cxl_extent *to_add)
> > +{
> > + struct cxl_extent_list_node *node = kzalloc(sizeof(*node), GFP_KERNEL);
> > + struct cxl_extent *extent;
> > +
> > + if (!node)
> > + return -ENOMEM;
> > + extent = kmemdup(to_add, sizeof(*extent), GFP_KERNEL);
>
> Maybe scatter some __free() magic in here and no_free_ptr() where
> they get handed off..
I changed stuct cxl_extent_list_node to hold struct cxl_extent itself
instead of a pointer to a separately allocated one. So:
struct cxl_extent_list_node {
struct cxl_extent extent; /* was: struct cxl_extent *extent; */
struct list_head list;
};
This function becomes:
static int stage_pending_extent(struct list_head *pending,
struct cxl_extent *to_add)
{
struct cxl_extent_list_node *node = kmalloc(sizeof(*node), GFP_KERNEL);
if (!node)
return -ENOMEM;
node->extent = *to_add;
list_add_tail(&node->list, pending);
return 0;
}
So a node can never be left in a partially built status that needs
cleaning up.
>
> > + if (!extent) {
> > + kfree(node);
> > + return -ENOMEM;
> > + }
> > +
> > + node->extent = extent;
> e.g.
> node->extent = no_free_ptr(extent);
>
> > + list_add_tail(&node->list, pending);
>
> list_add_tail(&no_free_ptr(node)->list, pending);
>
> > + return 0;
> > +}
>
> > +
> > +static void cxl_handle_dcd_event_records(struct cxl_memdev_state *mds,
> > + struct cxl_event_record_raw *raw_rec)
> > +{
> > + struct cxl_event_dcd *event = &raw_rec->event.dcd;
> > + struct cxl_extent *extent = &event->extent;
> > + struct device *dev = mds->cxlds.dev;
> > + uuid_t *id = &raw_rec->id;
> > + int rc;
> > +
> > + if (!uuid_equal(id, &CXL_EVENT_DC_EVENT_UUID))
> > + return;
> > +
> > + dev_dbg(dev, "DCD event %s : DPA:%#llx LEN:%#llx\n",
> > + cxl_dcd_evt_type_str(event->event_type),
> > + le64_to_cpu(extent->start_dpa), le64_to_cpu(extent->length));
> > +
> > + switch (event->event_type) {
> > + case DCD_ADD_CAPACITY:
> > + case DCD_RELEASE_CAPACITY:
> > + rc = handle_dcd_chain_event(mds, event);
> > + break;
> > + case DCD_FORCED_CAPACITY_RELEASE:
> > + dev_err_ratelimited(dev, "Forced release event ignored.\n");
>
> If that actually happens to us, maybe we should panic? All bets are off
> if we get one of these for capacity that was actually in use.
>
Yes, makes more sense to panic here:
panic("%s: DC capacity forcibly released\n", dev_name(dev));
> > + rc = 0;
> > + break;
> > + case DCD_REGION_CONFIGURATION_UPDATED:
> > + dev_dbg(dev, "Region configuration updated event ignored.\n");
> > + rc = 0;
> > + break;
> > + default:
> > + rc = -EINVAL;
> > + break;
> > + }
> > +
> > + if (rc)
> > + dev_err_ratelimited(dev, "dcd event failed: %d\n", rc);
> > +}
> > +
> > static void cxl_mem_get_records_log(struct cxl_memdev_state *mds,
> > enum cxl_event_log_type type)
> > {
> > @@ -1136,9 +1368,13 @@ static void cxl_mem_get_records_log(struct cxl_memdev_state *mds,
> > if (!nr_rec)
> > break;
> >
> > - for (i = 0; i < nr_rec; i++)
> > + for (i = 0; i < nr_rec; i++) {
> > __cxl_event_trace_record(cxlmd, type,
> > &payload->records[i]);
> > + if (type == CXL_EVENT_TYPE_DCD)
> > + cxl_handle_dcd_event_records(mds,
> > + &payload->records[i]);
>
> Just go long on that one. I wouldn't do the normal thing of flipping the condition
> because this being out of line makes sense as its an oddity in this function.
>
Done
> > + }
> >
> > if (payload->flags & CXL_GET_EVENT_FLAG_OVERFLOW)
> > trace_cxl_overflow(cxlmd, type, payload);
>
> > diff --git a/drivers/cxl/cxl.h b/drivers/cxl/cxl.h
> > index d098f7f50bef..64138e1768a1 100644
> > --- a/drivers/cxl/cxl.h
> > +++ b/drivers/cxl/cxl.h
> > @@ -12,6 +12,7 @@
> > #include <linux/node.h>
> > #include <linux/io.h>
> > #include <linux/range.h>
> > +#include <linux/xarray.h>
>
> This looks misplaces as nothing in the header changed
> that has anything to do with xarrays.
>
Oops. Moved to the patch that adds struct cxl_dc_tag_group, which tracks
extents in xarray.
Thanks,
Anisa
> > #include <cxl/cxl.h>
> >
> > extern const struct nvdimm_security_ops *cxl_security_ops;
> > @@ -557,6 +558,14 @@ struct cxl_pmem_region {
> > struct cxl_pmem_region_mapping mapping[];
> > };
> >
> > +/* See CXL r4.0 8.2.10.2.1.6 */
> > +enum dc_event {
> > + DCD_ADD_CAPACITY,
> > + DCD_RELEASE_CAPACITY,
> > + DCD_FORCED_CAPACITY_RELEASE,
> > + DCD_REGION_CONFIGURATION_UPDATED,
> > +};
> > +
> > struct cxl_dax_region {
> > struct device dev;
> > struct cxl_region *cxlr;
>