mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] cxl/region: Fix self-deadlock when cxl_memdev_attach_region() fails
@ 2026-11-17  2:59 Junho Cha via B4 Relay
  2026-11-17  8:08 ` Lucero Palau, Alejandro
  0 siblings, 1 reply; 2+ messages in thread
From: Junho Cha via B4 Relay @ 2026-11-17  2:59 UTC (permalink / raw)
  To: Dave Jiang, linux-cxl
  Cc: Davidlohr Bueso, Jonathan Cameron, Alison Schofield,
	Vishal Verma, Ira Weiny, Li Ming, Alejandro Lucero,
	Richard Cheng, linux-kernel, Junho Cha

From: Junho Cha <junho.cha@xcena.com>

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()          /* 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@xcena.com>
---
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@intel.com/
---
 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@xcena.com>



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-17  8:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17  2:59 [PATCH] cxl/region: Fix self-deadlock when cxl_memdev_attach_region() fails Junho Cha via B4 Relay
2026-11-17  8:08 ` Lucero Palau, Alejandro

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®