Re: [PATCH v2 6/8] cxl: Support for finding memory operation attributes from the current boot
From: Dan Williams
Date: Thu Mar 27 2025 - 13:44:25 EST
shiju.jose@ wrote:
> From: Shiju Jose <shiju.jose@xxxxxxxxxx>
>
> Certain operations on memory, such as memory repair, are permitted
> only when the address and other attributes for the operation are
> from the current boot. This is determined by checking whether the
> memory attributes for the operation match those in the CXL gen_media
> or CXL DRAM memory event records reported during the current boot.
>
> The CXL event records must be backed up because they are cleared
> in the hardware after being processed by the kernel.
>
> Support is added for storing CXL gen_media or CXL DRAM memory event
> records in xarrays. Additionally, helper functions are implemented
> to find a matching record in the xarray storage based on the memory
> attributes and repair type.
>
> Add validity check, when matching attributes for sparing, using
> the validity flag in the DRAM event record, to ensure that all
> required attributes for a requested repair operation are valid and
> set.
>
> Co-developed-by: Jonathan Cameron <Jonathan.Cameron@xxxxxxxxxx>
> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@xxxxxxxxxx>
> Signed-off-by: Shiju Jose <shiju.jose@xxxxxxxxxx>
> ---
> drivers/cxl/core/mbox.c | 11 ++-
> drivers/cxl/core/memdev.c | 9 +++
> drivers/cxl/core/ras.c | 145 ++++++++++++++++++++++++++++++++++++++
I thought we agreed to call the file edac.c since "ras" concepts are
spread throughout the driver?
> drivers/cxl/cxlmem.h | 46 ++++++++++++
> drivers/cxl/pci.c | 3 +
> 5 files changed, 212 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/cxl/core/mbox.c b/drivers/cxl/core/mbox.c
> index 19d46a284650..c9328f1b6464 100644
> --- a/drivers/cxl/core/mbox.c
> +++ b/drivers/cxl/core/mbox.c
> @@ -956,12 +956,19 @@ void cxl_event_trace_record(const struct cxl_memdev *cxlmd,
> hpa_alias = hpa - cache_size;
> }
>
> - if (event_type == CXL_CPER_EVENT_GEN_MEDIA)
> + if (event_type == CXL_CPER_EVENT_GEN_MEDIA) {
> + if (cxl_store_rec_gen_media((struct cxl_memdev *)cxlmd, evt))
> + dev_dbg(&cxlmd->dev, "CXL store rec_gen_media failed\n");
> +
All of this should be turned off when there is no EDAC consumer.
I don't see anything that triggers releasing the cache when a repair
makes continuing to save the information irrelevant.
I don't see any safety with respect to error storms and burning
unlimited memory in this cache.
The cache is storing 100% of the event record which seems wasteful if
100% of the data is not needed for validating repair operations.
The memory overhead expense of this feature needs to be estimated and
documented in the Kconfig so that distros can make a reasonable decision
about turning this on.
> trace_cxl_general_media(cxlmd, type, cxlr, hpa,
> hpa_alias, &evt->gen_media);
> - else if (event_type == CXL_CPER_EVENT_DRAM)
> + } else if (event_type == CXL_CPER_EVENT_DRAM) {
> + if (cxl_store_rec_dram((struct cxl_memdev *)cxlmd, evt))
> + dev_dbg(&cxlmd->dev, "CXL store rec_dram failed\n");
> +
> trace_cxl_dram(cxlmd, type, cxlr, hpa, hpa_alias,
> &evt->dram);
> + }
> }
> }
> EXPORT_SYMBOL_NS_GPL(cxl_event_trace_record, "CXL");
> diff --git a/drivers/cxl/core/memdev.c b/drivers/cxl/core/memdev.c
> index a16a5886d40a..bd9ba50bc01e 100644
> --- a/drivers/cxl/core/memdev.c
> +++ b/drivers/cxl/core/memdev.c
> @@ -25,8 +25,17 @@ static DEFINE_IDA(cxl_memdev_ida);
> static void cxl_memdev_release(struct device *dev)
> {
> struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
> + struct cxl_event_gen_media *rec_gen_media;
> + struct cxl_event_dram *rec_dram;
> + unsigned long index;
>
> ida_free(&cxl_memdev_ida, cxlmd->id);
> + xa_for_each(&cxlmd->rec_dram, index, rec_dram)
> + kfree(rec_dram);
> + xa_destroy(&cxlmd->rec_dram);
> + xa_for_each(&cxlmd->rec_gen_media, index, rec_gen_media)
> + kfree(rec_gen_media);
> + xa_destroy(&cxlmd->rec_gen_media);
> kfree(cxlmd);
> }
>
> diff --git a/drivers/cxl/core/ras.c b/drivers/cxl/core/ras.c
> index 485a831695c7..c703d4e7e05b 100644
> --- a/drivers/cxl/core/ras.c
> +++ b/drivers/cxl/core/ras.c
> @@ -7,6 +7,151 @@
> #include <cxlmem.h>
> #include "trace.h"
>
> +struct cxl_event_gen_media *
> +cxl_find_rec_gen_media(struct cxl_memdev *cxlmd,
> + struct cxl_mem_repair_attrbs *attrbs)
> +{
> + struct cxl_event_gen_media *rec;
> +
> + rec = xa_load(&cxlmd->rec_gen_media, attrbs->dpa);
> + if (!rec)
> + return NULL;
> +
> + if (attrbs->repair_type == CXL_PPR)
> + return rec;
> +
> + return NULL;
> +}
> +EXPORT_SYMBOL_NS_GPL(cxl_find_rec_gen_media, "CXL");
> +
> +struct cxl_event_dram *cxl_find_rec_dram(struct cxl_memdev *cxlmd,
> + struct cxl_mem_repair_attrbs *attrbs)
> +{
> + struct cxl_event_dram *rec;
> + u16 validity_flags;
> +
> + rec = xa_load(&cxlmd->rec_dram, attrbs->dpa);
> + if (!rec)
> + return NULL;
> +
> + validity_flags = get_unaligned_le16(rec->media_hdr.validity_flags);
> + if (!(validity_flags & CXL_DER_VALID_CHANNEL) ||
> + !(validity_flags & CXL_DER_VALID_RANK))
> + return NULL;
> +
> + switch (attrbs->repair_type) {
> + case CXL_PPR:
> + if (!(validity_flags & CXL_DER_VALID_NIBBLE) ||
> + get_unaligned_le24(rec->nibble_mask) == attrbs->nibble_mask)
> + return rec;
> + break;
> + case CXL_CACHELINE_SPARING:
> + if (!(validity_flags & CXL_DER_VALID_BANK_GROUP) ||
> + !(validity_flags & CXL_DER_VALID_BANK) ||
> + !(validity_flags & CXL_DER_VALID_ROW) ||
> + !(validity_flags & CXL_DER_VALID_COLUMN))
> + return NULL;
> +
> + if (rec->media_hdr.channel == attrbs->channel &&
> + rec->media_hdr.rank == attrbs->rank &&
> + rec->bank_group == attrbs->bank_group &&
> + rec->bank == attrbs->bank &&
> + get_unaligned_le24(rec->row) == attrbs->row &&
> + get_unaligned_le16(rec->column) == attrbs->column &&
> + (!(validity_flags & CXL_DER_VALID_NIBBLE) ||
> + get_unaligned_le24(rec->nibble_mask) ==
> + attrbs->nibble_mask) &&
> + (!(validity_flags & CXL_DER_VALID_SUB_CHANNEL) ||
> + rec->sub_channel == attrbs->sub_channel))
> + return rec;
> + break;
> + case CXL_ROW_SPARING:
> + if (!(validity_flags & CXL_DER_VALID_BANK_GROUP) ||
> + !(validity_flags & CXL_DER_VALID_BANK) ||
> + !(validity_flags & CXL_DER_VALID_ROW))
> + return NULL;
> +
> + if (rec->media_hdr.channel == attrbs->channel &&
> + rec->media_hdr.rank == attrbs->rank &&
> + rec->bank_group == attrbs->bank_group &&
> + rec->bank == attrbs->bank &&
> + get_unaligned_le24(rec->row) == attrbs->row &&
> + (!(validity_flags & CXL_DER_VALID_NIBBLE) ||
> + get_unaligned_le24(rec->nibble_mask) ==
> + attrbs->nibble_mask))
> + return rec;
> + break;
> + case CXL_BANK_SPARING:
> + if (!(validity_flags & CXL_DER_VALID_BANK_GROUP) ||
> + !(validity_flags & CXL_DER_VALID_BANK))
> + return NULL;
> +
> + if (rec->media_hdr.channel == attrbs->channel &&
> + rec->media_hdr.rank == attrbs->rank &&
> + rec->bank_group == attrbs->bank_group &&
> + rec->bank == attrbs->bank &&
> + (!(validity_flags & CXL_DER_VALID_NIBBLE) ||
> + get_unaligned_le24(rec->nibble_mask) ==
> + attrbs->nibble_mask))
> + return rec;
> + break;
> + case CXL_RANK_SPARING:
> + if (rec->media_hdr.channel == attrbs->channel &&
> + rec->media_hdr.rank == attrbs->rank &&
> + (!(validity_flags & CXL_DER_VALID_NIBBLE) ||
> + get_unaligned_le24(rec->nibble_mask) ==
> + attrbs->nibble_mask))
> + return rec;
> + break;
> + default:
> + return NULL;
> + }
> +
> + return NULL;
> +}
> +EXPORT_SYMBOL_NS_GPL(cxl_find_rec_dram, "CXL");
> +
> +int cxl_store_rec_gen_media(struct cxl_memdev *cxlmd, union cxl_event *evt)
> +{
> + void *old_rec;
> + struct cxl_event_gen_media *rec =
> + kmemdup(&evt->gen_media, sizeof(*rec), GFP_KERNEL);
> + if (!rec)
> + return -ENOMEM;
> +
> + old_rec = xa_store(&cxlmd->rec_gen_media,
> + le64_to_cpu(rec->media_hdr.phys_addr), rec,
> + GFP_KERNEL);
> + if (xa_is_err(old_rec))
> + return xa_err(old_rec);
> +
> + kfree(old_rec);
> +
> + return 0;
> +}
> +EXPORT_SYMBOL_NS_GPL(cxl_store_rec_gen_media, "CXL");
> +
> +int cxl_store_rec_dram(struct cxl_memdev *cxlmd, union cxl_event *evt)
> +{
> + void *old_rec;
> + struct cxl_event_dram *rec =
> + kmemdup(&evt->dram, sizeof(*rec), GFP_KERNEL);
> +
> + if (!rec)
> + return -ENOMEM;
> +
> + old_rec = xa_store(&cxlmd->rec_dram,
> + le64_to_cpu(rec->media_hdr.phys_addr), rec,
> + GFP_KERNEL);
> + if (xa_is_err(old_rec))
> + return xa_err(old_rec);
> +
> + kfree(old_rec);
> +
> + return 0;
> +}
> +EXPORT_SYMBOL_NS_GPL(cxl_store_rec_dram, "CXL");
> +
> static void cxl_cper_trace_corr_port_prot_err(struct pci_dev *pdev,
> struct cxl_ras_capability_regs ras_cap)
> {
> diff --git a/drivers/cxl/cxlmem.h b/drivers/cxl/cxlmem.h
> index 7ab257e0c85e..24ece579a145 100644
> --- a/drivers/cxl/cxlmem.h
> +++ b/drivers/cxl/cxlmem.h
> @@ -34,6 +34,41 @@
> (FIELD_GET(CXLMDEV_RESET_NEEDED_MASK, status) != \
> CXLMDEV_RESET_NEEDED_NOT)
>
> +enum cxl_mem_repair_type {
> + CXL_PPR,
> + CXL_CACHELINE_SPARING,
> + CXL_ROW_SPARING,
> + CXL_BANK_SPARING,
> + CXL_RANK_SPARING,
> + CXL_REPAIR_MAX,
> +};
> +
> +/**
> + * struct cxl_mem_repair_attrbs - CXL memory repair attributes
Between attr, param, attrbs the names of intermediate objects seem to
have no rhyme or reason in these patches. I don't have a specific
suggestion here beyond please take another pass over the whole set and
be consistent.
In general attr is an overloaded term especially in code files that have
sysfs attributes, so please steer away from "attr" for that reason.
> + * @dpa: DPA of memory to repair
> + * @nibble_mask: nibble mask, identifies one or more nibbles on the memory bus
> + * @row: row of memory to repair
> + * @column: column of memory to repair
> + * @channel: channel of memory to repair
> + * @sub_channel: sub channel of memory to repair
> + * @rank: rank of memory to repair
> + * @bank_group: bank group of memory to repair
> + * @bank: bank of memory to repair
> + * @repair_type: repair type. For eg. PPR, memory sparing etc.
> + */
> +struct cxl_mem_repair_attrbs {
> + u64 dpa;
> + u32 nibble_mask;
> + u32 row;
> + u16 column;
> + u8 channel;
> + u8 sub_channel;
> + u8 rank;
> + u8 bank_group;
> + u8 bank;
> + enum cxl_mem_repair_type repair_type;
> +};
> +
> /**
> * struct cxl_memdev - CXL bus object representing a Type-3 Memory Device
> * @dev: driver core device object
> @@ -45,6 +80,8 @@
> * @endpoint: connection to the CXL port topology for this memory device
> * @id: id number of this memdev instance.
> * @depth: endpoint port depth
> + * @rec_gen_media: xarray to store CXL general media records
> + * @rec_dram: xarray to store CXL DRAM records
> */
> struct cxl_memdev {
> struct device dev;
> @@ -56,6 +93,8 @@ struct cxl_memdev {
> struct cxl_port *endpoint;
> int id;
> int depth;
> + struct xarray rec_gen_media;
> + struct xarray rec_dram;
Can this move to an EDAC context object to not burden 'struct
cxl_memdev' by default?