Skip to contentSkip to conversation

cxl/edac: Fix event record lookups for memory repair

PATCH New Jongwon Lee 1 participant 4 messages Nov 26 CXL

lore
Share & tools
lore
#0 lore raw

v6 cover cxl/edac: Fix event record lookups for memory repair

This series fixes the cached event record lookups in the CXL memory repair path.

Patch 1 fixes a use-after-free that Sashiko reported on v2 [1]. The repair path reads cached DRAM records without a lock, and the event path can free them at the same time. The DRAM lookup now runs under the xarray lock, and the sparing path works on a copy of the record.

Patch 2 makes both lookups skip records that the device marked NOT_REPAIRABLE.

Patch 3 masks the DPA flag bits in the cache key, and depends on patch 2. With the masked key alone, the trace event DPA would also match NOT_REPAIRABLE records and let them be repaired.

I used xa_lock() for the race. kfree_rcu() would need a wrapper with an rcu_head around the record structs from the spec. Taking device_lock() in the repair path could deadlock with unbind, which removes the EDAC sysfs files while holding device_lock().

Changes in v6:
- Rebase on cxl/next (32af7819352b), no conflicts
- Patch 2: rename cxl_find_rec_gen_media() to cxl_check_for_rec_gen_media() and split the two tests (Jonathan)
- Patch 2: changelog wording for the online repair case (Alison)
- Patch 2: drop the Fixes: tag (Jonathan). Patch 3 keeps its Fixes:
tag, so a backport of patch 3 needs patch 2.
- Add the Reviewed-by tags from v5
- Cc Michal Schmidt for guard(xa_lock), as Jonathan suggested, and Ben Cheatham as a CXL reviewer

There is no guard(xa_lock) in the tree, and the 2024 patch for it [2] was not merged. v6 keeps xa_lock()/xa_unlock(), and I can convert these sites in a follow-up once a guard exists.

Changes in v5:
- New patch 2: NOT_REPAIRABLE check split out of v4 patch 2 and moved before the masking patch (Li Ming)
- Patch 3: rename cxl_rec_key() to cxl_rec_dpa() (Li Ming)
- Patch 1: drop the found variable in cxl_find_rec_dram() (Li Ming)
- Richard's Reviewed-by on v4 2/2 not carried over, as it was split
- Rebase on cxl/next (e44dc3a303)

Changes in v4:
- Patch 1: copy the whole struct cxl_event_dram out instead of only validity_flags (Richard Cheng)
- Patch 2: make cxl_rec_key() and cxl_rec_not_repairable() static inline (Richard Cheng)
- Cover letter: describe patch 1 first (Richard Cheng)

Changes in v3:
- New patch 1 for the record lifetime race (Sashiko)
- Patch 2: General Media lookup under the xarray lock, returns bool

Changes in v2:
- Moved the Claude Code note below the --- (Dave Jiang)

v5: https://lore.kernel.org/r/[email protected]
v4: https://lore.kernel.org/r/[email protected]
v3: https://lore.kernel.org/r/[email protected]
v2: https://lore.kernel.org/r/[email protected]
v1: https://lore.kernel.org/r/[email protected]

Tested in QEMU (master b574eaf) with a CXL type 3 device and a committed region, on cxl/next 32af7819352b, with KASAN, lockdep, kmemleak and preempt=full. Not tested on real CXL hardware.

- Repair requests for eight DPAs on all five mem_repair devices. With the series, 0x1000 and 0x2000 succeed, 0x5000 succeeds for PPR only, and NOT_REPAIRABLE (0x3000, 0x6000), missing (0x7000) and unmasked (0x1001, 0x3002) DPAs get -EINVAL. Unpatched, 0x1000 and PPR at 0x5000 fail, and 0x1001 and 0x3002 are accepted.
- Race test: event injection from the host, four guest repair loops, and a test-only mdelay(20) after the lookups. Unpatched, KASAN reports a slab-use-after-free in cxl_find_rec_dram() within seconds. With the series: 600 s, 10,950 repairs, 274,946 events, no report. Without the delay, on the previous base (2101c39473a0) with the same
code: 1,800 s, 98,220 repairs, 1,155,590 events, no report.
- Unbind test: cxl_mem unbound and rebound in a loop during repairs and event injection. 600 s, 78 cycles, 7,260 repairs, no report.
- kmemleak finds nothing after each run.
- edac.o builds with W=1 after each patch, sparse, clang W=1 (x86_64, arm64) and CONFIG_CXL_EDAC_MEM_REPAIR=n with no new warnings. The series also applies to cxl/fixes and v7.4-rc4.

This series was developed with Claude Code.

Thanks to Richard, Li Ming, Jonathan and Alison for the reviews.

[1] https://lore.kernel.org/r/[email protected]
[2] https://lore.kernel.org/all/[email protected]/

Jongwon Lee (3):
cxl/edac: Read cached DRAM event records under the xarray lock cxl/edac: Skip event records marked NOT_REPAIRABLE cxl/edac: Mask DPA flags in the event record cache key

 drivers/cxl/core/edac.c | 116 ++++++++++++++++++++++++++--------------
 1 file changed, 77 insertions(+), 39 deletions(-)

base-commit: 32af7819352bae9c690ebe277a44eceb501d5c68

Signature · 1 line
2.55.0
#1 lore raw

PATCH v6 1/3 cxl/edac: Read cached DRAM event records under the xarray lock

The event path caches DRAM event records in an xarray. It frees a record when xa_store() replaces it, when the record expires, and when the cache overflows. The event path runs with device_lock() and cxl_rwsem held for read.

The repair path looks up records with cxl_find_rec_dram() while holding only cxl_rwsem for read, so both paths can run at the same time. cxl_find_rec_dram() reads the record after xa_load() returns, and cxl_mem_perform_sparing() reads validity_flags from the returned record again later. Both reads can hit a record that the event path has just freed.

Move the lookup into __cxl_find_rec_dram(), which is called with the xarray lock held. cxl_find_rec_dram() now returns whether a record matched, and copies the matching record into a buffer passed in by the caller. The PPR path only needs to know whether there is a match, so it passes NULL. No caller keeps a pointer to the record after the lock is dropped. The event path frees a record only after xa_store() or xa_erase() has removed it from the xarray, and both of them take the same lock.

Fixes 0b5ccb0de1e2 ("cxl/edac: Support for finding memory operation attributes from the current boot")
Fixes 588ca944c277 ("cxl/edac: Add CXL memory device memory sparing control feature")
---
 drivers/cxl/core/edac.c | 70 +++++++++++++++++++++++++++--------------
 1 file changed, 47 insertions(+), 23 deletions(-)
drivers/cxl/core/edac.c +47 -23
index 56e50eea3994..bd217dccf119 100644
@@ -945,18 +945,17 @@ cxl_find_rec_gen_media(struct cxl_memdev *cxlmd,
 	return NULL;
 }
 
+/* Caller holds the xarray lock. The record may be freed once it is dropped. */
 static struct cxl_event_dram *
-cxl_find_rec_dram(struct cxl_memdev *cxlmd,
-		  struct cxl_mem_repair_attrbs *attrbs)
+__cxl_find_rec_dram(struct xarray *rec_xarray,
+		    struct cxl_mem_repair_attrbs *attrbs)
 {
-	struct cxl_mem_err_rec *array_rec = cxlmd->err_rec_array;
 	struct cxl_event_dram *rec;
 	u16 validity_flags;
 
-	if (!array_rec)
-		return NULL;
+	lockdep_assert_held(&rec_xarray->xa_lock);
 
-	rec = xa_load(&array_rec->rec_dram, attrbs->dpa);
+	rec = xa_load(rec_xarray, attrbs->dpa);
 	if (!rec)
 		return NULL;
 
@@ -1036,6 +1035,30 @@ cxl_find_rec_dram(struct cxl_memdev *cxlmd,
 	return NULL;
 }
 
+/*
+ * cxl_store_rec_dram() replaces and frees records while the repair path only
+ * holds cxl_rwsem for read, so look the record up and read it under the
+ * xarray lock, and hand back a copy of the record.
+ */
+static bool cxl_find_rec_dram(struct cxl_memdev *cxlmd,
+			      struct cxl_mem_repair_attrbs *attrbs,
+			      struct cxl_event_dram *out)
+{
+	struct cxl_mem_err_rec *array_rec = cxlmd->err_rec_array;
+	struct cxl_event_dram *rec;
+
+	if (!array_rec)
+		return false;
+
+	xa_lock(&array_rec->rec_dram);
+	rec = __cxl_find_rec_dram(&array_rec->rec_dram, attrbs);
+	if (rec && out)
+		*out = *rec;
+	xa_unlock(&array_rec->rec_dram);
+
+	return rec;
+}
+
 #define CXL_MAX_STORAGE_DAYS 10
 #define CXL_MAX_STORAGE_TIME_SECS (CXL_MAX_STORAGE_DAYS * 24 * 60 * 60)
 
@@ -1305,9 +1328,9 @@ cxl_mem_sparing_get_attrbs(struct cxl_mem_sparing_context *cxl_sparing_ctx)
 	return 0;
 }
 
-static struct cxl_event_dram *
-cxl_mem_get_rec_dram(struct cxl_memdev *cxlmd,
-		     struct cxl_mem_sparing_context *ctx)
+static bool cxl_mem_get_rec_dram(struct cxl_memdev *cxlmd,
+				 struct cxl_mem_sparing_context *ctx,
+				 struct cxl_event_dram *rec)
 {
 	struct cxl_mem_repair_attrbs attrbs = { 0 };
 
@@ -1339,10 +1362,10 @@ cxl_mem_get_rec_dram(struct cxl_memdev *cxlmd,
 		attrbs.repair_type = CXL_RANK_SPARING;
 		break;
 	default:
-		return NULL;
+		return false;
 	}
 
-	return cxl_find_rec_dram(cxlmd, &attrbs);
+	return cxl_find_rec_dram(cxlmd, &attrbs, rec);
 }
 
 static int
@@ -1351,7 +1374,8 @@ cxl_mem_perform_sparing(struct device *dev,
 {
 	struct cxl_memdev *cxlmd = cxl_sparing_ctx->cxlmd;
 	struct cxl_memdev_sparing_in_payload sparing_pi;
-	struct cxl_event_dram *rec = NULL;
+	struct cxl_event_dram rec;
+	bool rec_found = false;
 	u16 validity_flags = 0;
 	int ret;
 
@@ -1369,11 +1393,14 @@ cxl_mem_perform_sparing(struct device *dev,
 			return -EBUSY;
 	} else {
 		if (cxl_is_memdev_memory_online(cxlmd)) {
-			rec = cxl_mem_get_rec_dram(cxlmd, cxl_sparing_ctx);
-			if (!rec)
+			rec_found = cxl_mem_get_rec_dram(cxlmd, cxl_sparing_ctx,
+							 &rec);
+			if (!rec_found)
 				return -EINVAL;
 
-			if (!get_unaligned_le16(rec->media_hdr.validity_flags))
+			validity_flags =
+				get_unaligned_le16(rec.media_hdr.validity_flags);
+			if (!validity_flags)
 				return -EINVAL;
 		}
 	}
@@ -1383,13 +1410,10 @@ cxl_mem_perform_sparing(struct device *dev,
 	if (cxl_sparing_ctx->persist_mode)
 		sparing_pi.flags |= CXL_SET_HARD_SPARING(1);
 
-	if (rec)
-		validity_flags = get_unaligned_le16(rec->media_hdr.validity_flags);
-
 	switch (cxl_sparing_ctx->repair_type) {
 	case EDAC_REPAIR_CACHELINE_SPARING:
 		sparing_pi.column = cpu_to_le16(cxl_sparing_ctx->column);
-		if (!rec || (validity_flags & CXL_DER_VALID_SUB_CHANNEL)) {
+		if (!rec_found || (validity_flags & CXL_DER_VALID_SUB_CHANNEL)) {
 			sparing_pi.flags |= CXL_SET_SPARING_SUB_CHNL_VALID(1);
 			sparing_pi.sub_channel = cxl_sparing_ctx->sub_channel;
 		}
@@ -1406,9 +1430,9 @@ cxl_mem_perform_sparing(struct device *dev,
 		fallthrough;
 	default:
 		sparing_pi.channel = cxl_sparing_ctx->channel;
-		if ((rec && (validity_flags & CXL_DER_VALID_NIBBLE)) ||
-		    (!rec && (!cxl_sparing_ctx->nibble_mask ||
-			     (cxl_sparing_ctx->nibble_mask & 0xFFFFFF)))) {
+		if ((rec_found && (validity_flags & CXL_DER_VALID_NIBBLE)) ||
+		    (!rec_found && (!cxl_sparing_ctx->nibble_mask ||
+				    (cxl_sparing_ctx->nibble_mask & 0xFFFFFF)))) {
 			sparing_pi.flags |= CXL_SET_SPARING_NIB_MASK_VALID(1);
 			put_unaligned_le24(cxl_sparing_ctx->nibble_mask,
 					   sparing_pi.nibble_mask);
@@ -1820,7 +1844,7 @@ static int cxl_mem_perform_ppr(struct cxl_ppr_context *cxl_ppr_ctx)
 			attrbs.repair_type = CXL_PPR;
 			attrbs.dpa = cxl_ppr_ctx->dpa;
 			attrbs.nibble_mask = cxl_ppr_ctx->nibble_mask;
-			if (!cxl_find_rec_dram(cxlmd, &attrbs) &&
+			if (!cxl_find_rec_dram(cxlmd, &attrbs, NULL) &&
 			    !cxl_find_rec_gen_media(cxlmd, &attrbs))
 				return -EINVAL;
 		}
Signature · 1 line
2.55.0
#2 lore raw

PATCH v6 2/3 cxl/edac: Skip event records marked NOT_REPAIRABLE

In reply to Jongwon Lee

Bit 1 of the DPA field in General Media and DRAM event records is the NOT_REPAIRABLE flag. The device sets it when the memory at that DPA cannot be repaired.

cxl_find_rec_gen_media() and cxl_find_rec_dram() do not check this flag. A repair request that matches such a record passes the lookup and is sent to the device. Since the records are keyed by the raw DPA, this happens today when user space writes the DPA with bit 1 set.

Skip NOT_REPAIRABLE records in both lookups, so these records cannot satisfy the cached-event requirement for an online repair. The trace events already show dpa_flags='NOT_REPAIRABLE' for them, so user space can tell before it asks for a repair.

The next patch keys the records by the masked DPA. After that, the DPA from the trace event matches these records too, so the check has to be in place first.

cxl_find_rec_gen_media() now reads the record, so take the xarray lock there too, as cxl_find_rec_dram() does. Its only caller just checks the result, so make it return bool and rename it to cxl_check_for_rec_gen_media().

---
 drivers/cxl/core/edac.c | 30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)
drivers/cxl/core/edac.c +18 -12
index bd217dccf119..7aaf1242ba2f 100644
@@ -925,24 +925,30 @@ struct cxl_mem_repair_attrbs {
 	enum cxl_mem_repair_type repair_type;
 };
 
-static struct cxl_event_gen_media *
-cxl_find_rec_gen_media(struct cxl_memdev *cxlmd,
-		       struct cxl_mem_repair_attrbs *attrbs)
+static inline bool cxl_rec_not_repairable(const struct cxl_event_media_hdr *hdr)
+{
+	return le64_to_cpu(hdr->phys_addr) & CXL_DPA_NOT_REPAIRABLE;
+}
+
+static bool cxl_check_for_rec_gen_media(struct cxl_memdev *cxlmd,
+					struct cxl_mem_repair_attrbs *attrbs)
 {
 	struct cxl_mem_err_rec *array_rec = cxlmd->err_rec_array;
 	struct cxl_event_gen_media *rec;
+	bool found;
 
 	if (!array_rec)
-		return NULL;
+		return false;
 
-	rec = xa_load(&array_rec->rec_gen_media, attrbs->dpa);
-	if (!rec)
-		return NULL;
+	if (attrbs->repair_type != CXL_PPR)
+		return false;
 
-	if (attrbs->repair_type == CXL_PPR)
-		return rec;
+	xa_lock(&array_rec->rec_gen_media);
+	rec = xa_load(&array_rec->rec_gen_media, attrbs->dpa);
+	found = rec && !cxl_rec_not_repairable(&rec->media_hdr);
+	xa_unlock(&array_rec->rec_gen_media);
 
-	return NULL;
+	return found;
 }
 
 /* Caller holds the xarray lock. The record may be freed once it is dropped. */
@@ -956,7 +962,7 @@ __cxl_find_rec_dram(struct xarray *rec_xarray,
 	lockdep_assert_held(&rec_xarray->xa_lock);
 
 	rec = xa_load(rec_xarray, attrbs->dpa);
-	if (!rec)
+	if (!rec || cxl_rec_not_repairable(&rec->media_hdr))
 		return NULL;
 
 	validity_flags = get_unaligned_le16(rec->media_hdr.validity_flags);
@@ -1845,7 +1851,7 @@ static int cxl_mem_perform_ppr(struct cxl_ppr_context *cxl_ppr_ctx)
 			attrbs.dpa = cxl_ppr_ctx->dpa;
 			attrbs.nibble_mask = cxl_ppr_ctx->nibble_mask;
 			if (!cxl_find_rec_dram(cxlmd, &attrbs, NULL) &&
-			    !cxl_find_rec_gen_media(cxlmd, &attrbs))
+			    !cxl_check_for_rec_gen_media(cxlmd, &attrbs))
 				return -EINVAL;
 		}
 	}
Signature · 1 line
2.55.0
#3 lore raw

PATCH v6 3/3 cxl/edac: Mask DPA flags in the event record cache key

In reply to Jongwon Lee

The low bits of the DPA field in General Media and DRAM event records are flags. Bit 0 is VOLATILE and bit 1 is NOT_REPAIRABLE. The trace events print the DPA with CXL_DPA_MASK applied, while cxl_store_rec_gen_media() and cxl_store_rec_dram() use the raw value as the xarray key.

User space takes the DPA from the trace event and writes it to the mem_repair dpa attribute. If the record has any flag set, xa_load() does not find it and the repair fails with -EINVAL.

Use the masked DPA as the key. Records marked NOT_REPAIRABLE are still rejected, because the lookup skips them. This patch depends on "cxl/edac: Skip event records marked NOT_REPAIRABLE" for that check; if this patch is backported, that one has to go with it.

Fixes 0b5ccb0de1e2 ("cxl/edac: Support for finding memory operation attributes from the current boot")
---
 drivers/cxl/core/edac.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/cxl/core/edac.c +12 -4
index 7aaf1242ba2f..411d586c3484 100644
@@ -925,6 +925,16 @@ struct cxl_mem_repair_attrbs {
 	enum cxl_mem_repair_type repair_type;
 };
 
+/*
+ * The low bits of the event record DPA are flags (VOLATILE, NOT_REPAIRABLE).
+ * Records are looked up by the DPA user space writes back, which comes from
+ * the trace event and has the flags masked off, so key them the same way.
+ */
+static inline unsigned long cxl_rec_dpa(const struct cxl_event_media_hdr *hdr)
+{
+	return le64_to_cpu(hdr->phys_addr) & CXL_DPA_MASK;
+}
+
 static inline bool cxl_rec_not_repairable(const struct cxl_event_media_hdr *hdr)
 {
 	return le64_to_cpu(hdr->phys_addr) & CXL_DPA_NOT_REPAIRABLE;
@@ -1141,8 +1151,7 @@ int cxl_store_rec_gen_media(struct cxl_memdev *cxlmd, union cxl_event *evt)
 		return -ENOMEM;
 
 	old_rec = xa_store(&array_rec->rec_gen_media,
-			   le64_to_cpu(rec->media_hdr.phys_addr), rec,
-			   GFP_KERNEL);
+			   cxl_rec_dpa(&rec->media_hdr), rec, GFP_KERNEL);
 	if (xa_is_err(old_rec)) {
 		kfree(rec);
 		return xa_err(old_rec);
@@ -1171,8 +1180,7 @@ int cxl_store_rec_dram(struct cxl_memdev *cxlmd, union cxl_event *evt)
 		return -ENOMEM;
 
 	old_rec = xa_store(&array_rec->rec_dram,
-			   le64_to_cpu(rec->media_hdr.phys_addr), rec,
-			   GFP_KERNEL);
+			   cxl_rec_dpa(&rec->media_hdr), rec, GFP_KERNEL);
 	if (xa_is_err(old_rec)) {
 		kfree(rec);
 		return xa_err(old_rec);
Signature · 1 line
2.55.0