Skip to contentSkip to conversation

cxl/region: Restore a target's position from its slot after sorting

PATCH Applied Davidlohr Bueso 4 participants 4 messages Nov 6 – Nov 16 CXL

lore
Share & tools
lore

Davidlohr Bueso

author maintainer
#0 lore raw

PATCH v2 1/1 cxl/region: Restore a target's position from its slot after sorting

Region auto-discovery records each arriving endpoint decoder in the first free @targets slot and sets cxled->pos to that slot. Once every member has arrived, cxl_region_sort_targets() calculates each one's interleave position and sorts the array by it. The original version of that function then restored cxled->pos from the member's slot in the sorted array, so that follow-on code could rely on p->targets[cxled->pos] naming the member, failed sort or not.

Commit 0cf36a85c140 ("cxl/region: Use cxl_calc_interleave_pos() for auto-discovery") dropped that restore. cxled->pos now keeps whatever cxl_calc_interleave_pos() returned, which on failure is a negative errno. A member that could not be placed is left in a region it cannot be detached from by position: deleting that region runs __cxl_decoder_detach(), which finds pos < 0 and triggers:

cxl region0: expected mem0:decoder3.0 at position -6

  WARNING: drivers/cxl/core/region.c:2314 at __cxl_decoder_detach+0x483/0x910

A decoder committed by firmware behind a switch whose own decoders were not committed for the range is enough to get there

The same stale position is what commit cbda6a2c2bec ("cxl/region: Fix out-of-bounds access in cxl_cancel_auto_attach()") had to work around by walking the array instead of indexing it.

Restore cxled->pos from the member's slot after the sort.

Fixes 0cf36a85c140 ("cxl/region: Use cxl_calc_interleave_pos() for auto-discovery")

---
Found while battle testing BI changes.

Changes in v2:
- fix the hunk header

 drivers/cxl/core/region.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/cxl/core/region.c +8 -0
index a048bd5c06cb..d344bc4b5aab 100644
@@ -2045,6 +2045,14 @@ static int cxl_region_sort_targets(struct cxl_region *cxlr)
 	sort(p->targets, p->nr_targets, sizeof(p->targets[0]),
 	     cmp_interleave_pos, NULL);
 
+	/*
+	 * Restore cxled->pos from its ->targets[] slot so that follow-on
+	 * code paths can reliably do p->targets[cxled->pos] to
+	 * self-reference their entry, whether or not the sort succeeded.
+	 */
+	for (i = 0; i < p->nr_targets; i++)
+		p->targets[i]->pos = i;
+
 	dev_dbg(&cxlr->dev, "region sort %s\n", rc ? "failed" : "successful");
 	return rc;
 }
Signature · 1 line
2.39.5
Reviewed-by on v2 1/1
#1 lore raw
Quoted 25 lines · Davidlohr Bueso

Region auto-discovery records each arriving endpoint decoder in the first free @targets slot and sets cxled->pos to that slot. Once every member has arrived, cxl_region_sort_targets() calculates each one's interleave position and sorts the array by it. The original version of that function then restored cxled->pos from the member's slot in the sorted array, so that follow-on code could rely on p->targets[cxled->pos] naming the member, failed sort or not.

Commit 0cf36a85c140 ("cxl/region: Use cxl_calc_interleave_pos() for auto-discovery") dropped that restore. cxled->pos now keeps whatever cxl_calc_interleave_pos() returned, which on failure is a negative errno. A member that could not be placed is left in a region it cannot be detached from by position: deleting that region runs __cxl_decoder_detach(), which finds pos < 0 and triggers:

cxl region0: expected mem0:decoder3.0 at position -6

  WARNING: drivers/cxl/core/region.c:2314 at __cxl_decoder_detach+0x483/0x910

A decoder committed by firmware behind a switch whose own decoders were not committed for the range is enough to get there

The same stale position is what commit cbda6a2c2bec ("cxl/region: Fix out-of-bounds access in cxl_cancel_auto_attach()") had to work around by walking the array instead of indexing it.

Quoted 6 lines

Restore cxled->pos from the member's slot after the sort.

Fixes 0cf36a85c140 ("cxl/region: Use cxl_calc_interleave_pos() for auto-discovery")

---

Thanks,
Ben

reviewer Reviewed-by on v2 1/1
#2 lore raw
In reply to Davidlohr Bueso
Quoted 25 lines · Davidlohr Bueso

Region auto-discovery records each arriving endpoint decoder in the first free @targets slot and sets cxled->pos to that slot. Once every member has arrived, cxl_region_sort_targets() calculates each one's interleave position and sorts the array by it. The original version of that function then restored cxled->pos from the member's slot in the sorted array, so that follow-on code could rely on p->targets[cxled->pos] naming the member, failed sort or not.

Commit 0cf36a85c140 ("cxl/region: Use cxl_calc_interleave_pos() for auto-discovery") dropped that restore. cxled->pos now keeps whatever cxl_calc_interleave_pos() returned, which on failure is a negative errno. A member that could not be placed is left in a region it cannot be detached from by position: deleting that region runs __cxl_decoder_detach(), which finds pos < 0 and triggers:

cxl region0: expected mem0:decoder3.0 at position -6

   WARNING: drivers/cxl/core/region.c:2314 at __cxl_decoder_detach+0x483/0x910

A decoder committed by firmware behind a switch whose own decoders were not committed for the range is enough to get there

The same stale position is what commit cbda6a2c2bec ("cxl/region: Fix out-of-bounds access in cxl_cancel_auto_attach()") had to work around by walking the array instead of indexing it.

Quoted 5 lines

Restore cxled->pos from the member's slot after the sort.

Fixes 0cf36a85c140 ("cxl/region: Use cxl_calc_interleave_pos() for auto-discovery")
Quoted 28 lines

---
Found while battle testing BI changes.

Changes in v2:
- fix the hunk header

  drivers/cxl/core/region.c | 8 ++++++++
  1 file changed, 8 insertions(+)
drivers/cxl/core/region.c +8 -0
index a048bd5c06cb..d344bc4b5aab 100644
@@ -2045,6 +2045,14 @@ static int cxl_region_sort_targets(struct cxl_region *cxlr)
  	sort(p->targets, p->nr_targets, sizeof(p->targets[0]),
  	     cmp_interleave_pos, NULL);
 
+	/*
+	 * Restore cxled->pos from its ->targets[] slot so that follow-on
+	 * code paths can reliably do p->targets[cxled->pos] to
+	 * self-reference their entry, whether or not the sort succeeded.
+	 */
+	for (i = 0; i < p->nr_targets; i++)
+		p->targets[i]->pos = i;
+
  	dev_dbg(&cxlr->dev, "region sort %s\n", rc ? "failed" : "successful");
  	return rc;
  }
maintainer Applied
#3 lore raw
In reply to Davidlohr Bueso
Quoted 25 lines · Davidlohr Bueso

Region auto-discovery records each arriving endpoint decoder in the first free @targets slot and sets cxled->pos to that slot. Once every member has arrived, cxl_region_sort_targets() calculates each one's interleave position and sorts the array by it. The original version of that function then restored cxled->pos from the member's slot in the sorted array, so that follow-on code could rely on p->targets[cxled->pos] naming the member, failed sort or not.

Commit 0cf36a85c140 ("cxl/region: Use cxl_calc_interleave_pos() for auto-discovery") dropped that restore. cxled->pos now keeps whatever cxl_calc_interleave_pos() returned, which on failure is a negative errno. A member that could not be placed is left in a region it cannot be detached from by position: deleting that region runs __cxl_decoder_detach(), which finds pos < 0 and triggers:

cxl region0: expected mem0:decoder3.0 at position -6

  WARNING: drivers/cxl/core/region.c:2314 at __cxl_decoder_detach+0x483/0x910

A decoder committed by firmware behind a switch whose own decoders were not committed for the range is enough to get there

The same stale position is what commit cbda6a2c2bec ("cxl/region: Fix out-of-bounds access in cxl_cancel_auto_attach()") had to work around by walking the array instead of indexing it.

Quoted 5 lines

Restore cxled->pos from the member's slot after the sort.

Fixes 0cf36a85c140 ("cxl/region: Use cxl_calc_interleave_pos() for auto-discovery")

Applied to cxl/fixes
1731c729762b

Quoted 28 lines

---
Found while battle testing BI changes.

Changes in v2:
- fix the hunk header

 drivers/cxl/core/region.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/cxl/core/region.c +8 -0
index a048bd5c06cb..d344bc4b5aab 100644
@@ -2045,6 +2045,14 @@ static int cxl_region_sort_targets(struct cxl_region *cxlr)
 	sort(p->targets, p->nr_targets, sizeof(p->targets[0]),
 	     cmp_interleave_pos, NULL);
 
+	/*
+	 * Restore cxled->pos from its ->targets[] slot so that follow-on
+	 * code paths can reliably do p->targets[cxled->pos] to
+	 * self-reference their entry, whether or not the sort succeeded.
+	 */
+	for (i = 0; i < p->nr_targets; i++)
+		p->targets[i]->pos = i;
+
 	dev_dbg(&cxlr->dev, "region sort %s\n", rc ? "failed" : "successful");
 	return rc;
 }