Skip to contentSkip to conversation

cxl/region: Keep a decoder staged when its auto attach fails

PATCH In review Dave Jiang 3 participants 3 messages Nov 20 CXL

lore
Share & tools
lore

Dave Jiang

author maintainer
#0 lore raw

PATCH 1/1 cxl/region: Keep a decoder staged when its auto attach fails

An auto region lists each arriving endpoint decoder as a target in the AUTO_STAGED state. Once all targets are there, cxl_region_attach_position() attaches each one port by port. Attaching at the endpoint port sets cxld->region and moves the decoder to AUTO. If a later port fails, the unwind clears cxld->region again, but the decoder stays in AUTO and stays listed as a target.

When that endpoint goes away, __cxl_decoder_detach() finds no region and calls cxl_cancel_auto_attach(), which only removes AUTO_STAGED decoders. The freed decoder is left in the region's target list, and the next teardown of the region reads it:

  BUG: KASAN: slab-use-after-free in __cxl_decoder_detach+0xa6/0x3b0 [cxl_core]
   cxl_decoder_detach+0x6c/0xf0 [cxl_core]
   unregister_region+0xcb/0x470 [cxl_core]
   kill_regions+0x9f/0x100 [cxl_core]

Put the decoder back in AUTO_STAGED when the unwind leaves it with no region.

Fixes 87805c32e6ad ("cxl/region: Fix use-after-free from auto assembly failure")
---
 drivers/cxl/core/region.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/cxl/core/region.c +8 -0
index a048bd5c06cb..f1083976592f 100644
@@ -1830,6 +1830,14 @@ static int cxl_region_attach_position(struct cxl_region *cxlr,
 	for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
 	     iter = to_cxl_port(iter->dev.parent))
 		cxl_port_detach_region(iter, cxlr, cxled);
+
+	/*
+	 * Attaching at the endpoint port moved a staged decoder to AUTO, and
+	 * the unwind above cleared its region again. It is still listed as a
+	 * target, so keep it staged for cxl_cancel_auto_attach() to find.
+	 */
+	if (cxled->state == CXL_DECODER_STATE_AUTO && !cxled->cxld.region)
+		cxled->state = CXL_DECODER_STATE_AUTO_STAGED;
 	return rc;
 }
 

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3

Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [High] cxl/region: duplicate target attach…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] cxl/region: duplicate target attach corrupts region refs in cxl_region_attach() [1]
--

commit bb73ffca81dd406b180098a77a7d18645c37f8a1
Author: Dave Jiang <[email protected]>

cxl/region: Keep a decoder staged when its auto attach fails

This commit restores an endpoint decoder's state to CXL_DECODER_STATE_AUTO_STAGED if cxl_region_attach_position() fails and unwinds its region attachment.

Quoted 8 lines
drivers/cxl/core/region.c +0 -0
index a048bd5c06cb6..f1083976592f9 100644
@@ -1830,6 +1830,14 @@ static int cxl_region_attach_position(struct cxl_region *cxlr,
 	for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
 	     iter = to_cxl_port(iter->dev.parent))
 		cxl_port_detach_region(iter, cxlr, cxled);

drivers/cxl/core/region.c static int cxl_region_attach_position(struct cxl_region *cxlr, · line 1842

Quoted 10 lines
drivers/cxl/core/region.c +8 -0
+
+	/*
+	 * Attaching at the endpoint port moved a staged decoder to AUTO, and
+	 * the unwind above cleared its region again. It is still listed as a
+	 * target, so keep it staged for cxl_cancel_auto_attach() to find.
+	 */
+	if (cxled->state == CXL_DECODER_STATE_AUTO && !cxled->cxld.region)
+		cxled->state = CXL_DECODER_STATE_AUTO_STAGED;
 	return rc;
 }

[Severity: High]
This problem wasn't introduced by this patch, but can a duplicate write to a targetN sysfs attribute cause this unwind loop to detach an already attached target [1]?

In cxl_region_validate_position(), if p->targets[pos] == cxled, it returns 0:

drivers/cxl/core/region.c:cxl_region_validate_position() {
    ...
	if (p->targets[pos] == cxled)
		return 0;
    ...
}

cxl_region_attach() treats that 0 return as success and calls cxl_region_attach_position() a second time for the same decoder instead of returning early. When configuring a manual region with interleave_ways >= 2, attaching succeeds again at the endpoint port (incrementing nr_eps to 2) and then fails at the parent port (for example with -EBUSY in cxl_rr_ep_add() because cxled is already in cxl_rr->endpoints).

The err unwind in cxl_region_attach_position() then detaches cxled across all ports up to the root and frees the ancestor ports' cxl_region_ref while p->targets[pos] remains set. Once the remaining targets are attached, cxl_port_setup_targets() gets NULL from cxl_rr_load() on the ancestor port and dereferences cxl_rr->decoder.

[1] https://sashiko.dev/#/bug/linux-bc3dac83-96b6-4b99-8e2f-b2f4ad93b98b

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
Quoted 6 lines · On Fri, Nov 20, 2026 at 06:39:10PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] cxl/region: duplicate target attach corrupts region refs in cxl_region_attach() [1]
--

This is true, yet we already have a fix for it in cxl/fixes:

cxl/region: Reject a repeat write of an endpoint decoder to targetN
https://git.kernel.org/pub/scm/linux/kernel/git/cxl/cxl.git/commit/?h=fixes&id=b8183f9259b55fd79ecd0f2039bb4e9393eac936

with that in place this complaint goes away.

-- Alison

Quoted 62 lines

commit bb73ffca81dd406b180098a77a7d18645c37f8a1
Author: Dave Jiang <[email protected]>

cxl/region: Keep a decoder staged when its auto attach fails

This commit restores an endpoint decoder's state to CXL_DECODER_STATE_AUTO_STAGED if cxl_region_attach_position() fails and unwinds its region attachment.

Quoted 8 lines
drivers/cxl/core/region.c +0 -0
index a048bd5c06cb6..f1083976592f9 100644
@@ -1830,6 +1830,14 @@ static int cxl_region_attach_position(struct cxl_region *cxlr,
 	for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
 	     iter = to_cxl_port(iter->dev.parent))
 		cxl_port_detach_region(iter, cxlr, cxled);

drivers/cxl/core/region.c static int cxl_region_attach_position(struct cxl_region *cxlr, · line 1842

Quoted 10 lines
drivers/cxl/core/region.c +8 -0
+
+	/*
+	 * Attaching at the endpoint port moved a staged decoder to AUTO, and
+	 * the unwind above cleared its region again. It is still listed as a
+	 * target, so keep it staged for cxl_cancel_auto_attach() to find.
+	 */
+	if (cxled->state == CXL_DECODER_STATE_AUTO && !cxled->cxld.region)
+		cxled->state = CXL_DECODER_STATE_AUTO_STAGED;
 	return rc;
 }

[Severity: High]
This problem wasn't introduced by this patch, but can a duplicate write to a targetN sysfs attribute cause this unwind loop to detach an already attached target [1]?

In cxl_region_validate_position(), if p->targets[pos] == cxled, it returns 0:

drivers/cxl/core/region.c:cxl_region_validate_position() {
    ...
	if (p->targets[pos] == cxled)
		return 0;
    ...
}

cxl_region_attach() treats that 0 return as success and calls cxl_region_attach_position() a second time for the same decoder instead of returning early. When configuring a manual region with interleave_ways >= 2, attaching succeeds again at the endpoint port (incrementing nr_eps to 2) and then fails at the parent port (for example with -EBUSY in cxl_rr_ep_add() because cxled is already in cxl_rr->endpoints).

The err unwind in cxl_region_attach_position() then detaches cxled across all ports up to the root and frees the ancestor ports' cxl_region_ref while p->targets[pos] remains set. Once the remaining targets are attached, cxl_port_setup_targets() gets NULL from cxl_rr_load() on the ancestor port and dereferences cxl_rr->decoder.

[1] https://sashiko.dev/#/bug/linux-bc3dac83-96b6-4b99-8e2f-b2f4ad93b98b

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1