Re: [PATCH] cxl/region: Fix self-deadlock when cxl_memdev_attach_region() fails

From: Lucero Palau, Alejandro

Date: Tue Nov 17 2026 - 03:09:08 EST


Hi Junho,

On 17/11/2026 02:59, Junho Cha via B4 Relay wrote:
From: Junho Cha <junho.cha@xxxxxxxxx>

cxl_memdev_attach_region() holds cxl_rwsem.region for read while it
registers endpoint_unregister_region() with devm_add_action_or_reset().
If registering the action fails, devm_add_action_or_reset() runs the
action at once, which reaches cxl_decoder_detach() through
unregister_region() and detach_target():

cxl_memdev_attach_region()
down_read(&cxl_rwsem.region)
devm_add_action_or_reset()


Thanks for giving visibility to this issue.


I have been working on something related to this complex unwinding of things, not when an error during initialization happens like this case, but under expected unwinding through the different possible cases. Apart from being complex and hard to get everything properly sorted out, the way the unwinding is programmed is semantically wrong for Type2 and only safe due to the restrictions with current Type2 expectations which will be overcome soon. I am close to send an RFC about that change implying this specific action will be different. I need to study the implications for the code I have, but as this will take some discussion and versions until is hopefully accepted, your patch is the right thing to do now.


Thanks!


Reviewed-by: Alejandro Lucero <alucerop@xxxxxxx>


/* fails with -ENOMEM */
endpoint_unregister_region()
unregister_region()
detach_target()
cxl_decoder_detach()
down_write_killable(&cxl_rwsem.region)

The task then waits for a write lock while holding the same rwsem for
read, and never makes progress. Before it hangs, lockdep also reports
a circular dependency: endpoint_unregister_region() takes
cxlrd->regions_lock under cxl_rwsem.dpa, while cxl_add_to_region()
takes cxlrd->regions_lock before both rwsems.

Hold cxl_rwsem.region and cxl_rwsem.dpa only while finding and
validating the region, take the region reference and the HPA range
under them, and register the action after dropping them. The endpoint
device lock stays held across the registration, as before.

Reproduced with cxl_test (type2_test=1) by failing the devres
allocation through failslab, with the stack filter limited to
cxl_memdev_attach_region(). Without this patch the probing kworker
hangs in cxl_decoder_detach(). With it, the cxl_mem probe fails with
-ENOMEM and lockdep stays quiet.

Fixes: d8dcb0b74b04 ("cxl/region: Introduce devm_cxl_probe_mem()")
Signed-off-by: Junho Cha <junho.cha@xxxxxxxxx>
---
Dave pointed at this reset path while reviewing the Type2 multipf
series [1]. This is a standalone fix for the existing code that keeps
the region teardown when registering the action fails.

[1] https://lore.kernel.org/linux-cxl/6bc33514-8bfb-44d6-8fde-28f45dff5eb9@xxxxxxxxx/
---
drivers/cxl/core/region.c | 73 +++++++++++++++++++++++++++--------------------
1 file changed, 42 insertions(+), 31 deletions(-)

diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
index 4cd9bb5dfd61..b7471a3aca12 100644
--- a/drivers/cxl/core/region.c
+++ b/drivers/cxl/core/region.c
@@ -4424,55 +4424,66 @@ int cxl_memdev_attach_region(struct cxl_memdev *cxlmd)
struct cxl_port *endpoint = cxlmd->endpoint;
struct cxl_endpoint_decoder *cxled;
struct cxl_region *cxlr;
+ struct range hpa_range;
int rc;
/* hold endpoint lock to setup autoremove of the region */
guard(device)(&endpoint->dev);
if (!endpoint->dev.driver)
return -ENXIO;
- guard(rwsem_read)(&cxl_rwsem.region);
- guard(rwsem_read)(&cxl_rwsem.dpa);
- /*
- * TODO auto-instantiate a region, for now assume this will find an
- * auto-region
- */
- struct device *dev __free(put_device) =
- device_find_child(&endpoint->dev, NULL, first_mapped_decoder);
+ scoped_guard(rwsem_read, &cxl_rwsem.region) {
+ guard(rwsem_read)(&cxl_rwsem.dpa);
- if (!dev) {
- dev_dbg(cxlmd->cxlds->dev, "no region found for memdev %s\n",
- dev_name(&cxlmd->dev));
- return -ENXIO;
- }
+ /*
+ * TODO auto-instantiate a region, for now assume this will
+ * find an auto-region
+ */
+ struct device *dev __free(put_device) =
+ device_find_child(&endpoint->dev, NULL,
+ first_mapped_decoder);
+
+ if (!dev) {
+ dev_dbg(cxlmd->cxlds->dev,
+ "no region found for memdev %s\n",
+ dev_name(&cxlmd->dev));
+ return -ENXIO;
+ }
- cxled = to_cxl_endpoint_decoder(dev);
- cxlr = cxled->cxld.region;
+ cxled = to_cxl_endpoint_decoder(dev);
+ cxlr = cxled->cxld.region;
- if (cxlr->params.state < CXL_CONFIG_COMMIT) {
- dev_dbg(cxlmd->cxlds->dev,
- "region %s not committed for memdev %s\n",
- dev_name(&cxlr->dev), dev_name(&cxlmd->dev));
- return -ENXIO;
- }
+ if (cxlr->params.state < CXL_CONFIG_COMMIT) {
+ dev_dbg(cxlmd->cxlds->dev,
+ "region %s not committed for memdev %s\n",
+ dev_name(&cxlr->dev), dev_name(&cxlmd->dev));
+ return -ENXIO;
+ }
- if (cxlr->params.nr_targets > 1) {
- dev_dbg(cxlmd->cxlds->dev,
- "Only attach to local non-interleaved region\n");
- return -ENXIO;
+ if (cxlr->params.nr_targets > 1) {
+ dev_dbg(cxlmd->cxlds->dev,
+ "Only attach to local non-interleaved region\n");
+ return -ENXIO;
+ }
+
+ /* Only teardown regions that pass validation */
+ get_device(&cxlr->dev);
+ hpa_range = (struct range) {
+ .start = cxlr->params.res->start,
+ .end = cxlr->params.res->end,
+ };
}
- /* Only teardown regions that pass validation, ignore the rest */
- get_device(&cxlr->dev);
+ /*
+ * On failure the action runs immediately and detaching the region
+ * takes cxl_rwsem.region for write, so add it without the rwsem held.
+ */
rc = devm_add_action_or_reset(&endpoint->dev,
endpoint_unregister_region, cxlr);
if (rc)
return rc;
- attach->hpa_range = (struct range) {
- .start = cxlr->params.res->start,
- .end = cxlr->params.res->end,
- };
+ attach->hpa_range = hpa_range;
return 0;
}
EXPORT_SYMBOL_FOR_MODULES(cxl_memdev_attach_region, "cxl_mem");

---
base-commit: 2101c39473a06ee551a8cb4d549a1112aed324ad
change-id: 20261113-junhomax-fix-cxl-attach-region-deadlock-fc0a425f7265

Best regards,
--
Junho Cha <junho.cha@xxxxxxxxx>