* [PATCH v2] cxl/region: Fix self-deadlock when cxl_memdev_attach_region() fails
@ 2026-11-19 1:44 Junho Cha via B4 Relay
2026-12-03 0:52 ` 차준호
2026-12-03 20:50 ` Alison Schofield
0 siblings, 2 replies; 3+ messages in thread
From: Junho Cha via B4 Relay @ 2026-11-19 1:44 UTC (permalink / raw)
To: Dave Jiang, linux-cxl
Cc: Davidlohr Bueso, Jonathan Cameron, Alison Schofield,
Vishal Verma, Ira Weiny, Li Ming, Ben Cheatham, Alejandro Lucero,
Richard Cheng, linux-kernel, Sashiko, 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.
Register the action with devm_add_action() instead, still under both
rwsems, and if that fails, run it only after the rwsems are dropped.
The endpoint device lock stays held throughout, 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 task hangs
in cxl_decoder_detach(). With it, the cxl_mem probe fails with
-ENOMEM, the region is unregistered, and lockdep stays quiet.
Fixes: d8dcb0b74b04 ("cxl/region: Introduce devm_cxl_probe_mem()")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Link: https://lore.kernel.org/all/20260805080537.7489E1F000E9@smtp.kernel.org/
Assisted-by: LLM
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/
---
Changes in v2:
- Register the action under both rwsems as before and only run the
unwind after dropping them. v1 left a window where the region could
be detached before the action was registered (Ben).
- Add Reported-by/Link for Sashiko.
- Drop Alejandro's Reviewed-by, since the fix changed.
- Link to v1: https://patch.msgid.link/20261117-junhomax-fix-cxl-attach-region-deadlock-v1-1-f373a135edd2@xcena.com
---
drivers/cxl/core/region.c | 32 ++++++++++++++++++++++++--------
1 file changed, 24 insertions(+), 8 deletions(-)
diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
index 237172b33f55..f9538ac98a1f 100644
--- a/drivers/cxl/core/region.c
+++ b/drivers/cxl/core/region.c
@@ -4437,7 +4437,8 @@ static int first_mapped_decoder(struct device *dev, const void *data)
* Runs in cxl_mem_probe context after successful endpoint probe, assumes the
* simple case of single mapped decoder per memdev.
*/
-int cxl_memdev_attach_region(struct cxl_memdev *cxlmd)
+static int __cxl_memdev_attach_region(struct cxl_memdev *cxlmd,
+ struct cxl_region **unwind)
{
struct cxl_attach_region *attach =
container_of(cxlmd->attach, typeof(*attach), attach);
@@ -4446,10 +4447,6 @@ int cxl_memdev_attach_region(struct cxl_memdev *cxlmd)
struct cxl_region *cxlr;
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);
@@ -4484,10 +4481,11 @@ int cxl_memdev_attach_region(struct cxl_memdev *cxlmd)
/* Only teardown regions that pass validation, ignore the rest */
get_device(&cxlr->dev);
- rc = devm_add_action_or_reset(&endpoint->dev,
- endpoint_unregister_region, cxlr);
- if (rc)
+ rc = devm_add_action(&endpoint->dev, endpoint_unregister_region, cxlr);
+ if (rc) {
+ *unwind = cxlr;
return rc;
+ }
attach->hpa_range = (struct range) {
.start = cxlr->params.res->start,
@@ -4495,6 +4493,24 @@ int cxl_memdev_attach_region(struct cxl_memdev *cxlmd)
};
return 0;
}
+
+int cxl_memdev_attach_region(struct cxl_memdev *cxlmd)
+{
+ struct cxl_port *endpoint = cxlmd->endpoint;
+ struct cxl_region *unwind = NULL;
+ int rc;
+
+ /* hold endpoint lock to setup autoremove of the region */
+ guard(device)(&endpoint->dev);
+ if (!endpoint->dev.driver)
+ return -ENXIO;
+
+ /* Detaching the region takes cxl_rwsem.region, so unwind without it */
+ rc = __cxl_memdev_attach_region(cxlmd, &unwind);
+ if (unwind)
+ endpoint_unregister_region(unwind);
+ return rc;
+}
EXPORT_SYMBOL_FOR_MODULES(cxl_memdev_attach_region, "cxl_mem");
/*
---
base-commit: 32af7819352bae9c690ebe277a44eceb501d5c68
change-id: 20261113-junhomax-fix-cxl-attach-region-deadlock-fc0a425f7265
Best regards,
--
Junho Cha <junho.cha@xcena.com>
^ permalink raw reply [flat|nested] 3+ messages in thread* RE: [PATCH v2] cxl/region: Fix self-deadlock when cxl_memdev_attach_region() fails
2026-11-19 1:44 [PATCH v2] cxl/region: Fix self-deadlock when cxl_memdev_attach_region() fails Junho Cha via B4 Relay
@ 2026-12-03 0:52 ` 차준호
2026-12-03 20:50 ` Alison Schofield
1 sibling, 0 replies; 3+ messages in thread
From: 차준호 @ 2026-12-03 0:52 UTC (permalink / raw)
To: Dave Jiang, linux-cxl
Cc: Davidlohr Bueso, Jonathan Cameron, Alison Schofield,
Vishal Verma, Ira Weiny, Li Ming, Ben Cheatham, Alejandro Lucero,
Richard Cheng, linux-kernel, Sashiko
Hi Dave,
Gentle ping on this one. It still applies cleanly to cxl/next and
cxl/fixes.
The same error path is unchanged on your type2-revoke branch, and the
patch applies there with a 3-way merge.
Ben, does this address your comments on v1?
Alejandro, v2 changed the fix after Ben's review, so I dropped your
Reviewed-by. Could you take another look?
Thanks,
Junho
-----Original Message-----
From: Junho Cha via B4 Relay <devnull+junho.cha.xcena.com@kernel.org>
Sent: Thursday, November 19, 2026 10:45 AM
To: Dave Jiang <dave.jiang@intel.com>; linux-cxl@vger.kernel.org
Cc: Davidlohr Bueso <dave@stgolabs.net>; Jonathan Cameron <jic23@kernel.org>; Alison Schofield <alison.schofield@intel.com>; Vishal Verma <vishal.l.verma@intel.com>; Ira Weiny <iweiny@kernel.org>; Li Ming <ming.li@zohomail.com>; Ben Cheatham <benjamin.cheatham@amd.com>; Alejandro Lucero <alucerop@amd.com>; Richard Cheng <icheng@nvidia.com>; linux-kernel@vger.kernel.org; Sashiko <sashiko-bot@kernel.org>; 차준호 <junho.cha@xcena.com>
Subject: [PATCH v2] cxl/region: Fix self-deadlock when cxl_memdev_attach_region() fails
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.
Register the action with devm_add_action() instead, still under both rwsems, and if that fails, run it only after the rwsems are dropped.
The endpoint device lock stays held throughout, 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 task hangs in cxl_decoder_detach(). With it, the cxl_mem probe fails with -ENOMEM, the region is unregistered, and lockdep stays quiet.
Fixes: d8dcb0b74b04 ("cxl/region: Introduce devm_cxl_probe_mem()")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Link: https://lore.kernel.org/all/20260805080537.7489E1F000E9@smtp.kernel.org/
Assisted-by: LLM
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/
---
Changes in v2:
- Register the action under both rwsems as before and only run the
unwind after dropping them. v1 left a window where the region could
be detached before the action was registered (Ben).
- Add Reported-by/Link for Sashiko.
- Drop Alejandro's Reviewed-by, since the fix changed.
- Link to v1: https://patch.msgid.link/20261117-junhomax-fix-cxl-attach-region-deadlock-v1-1-f373a135edd2@xcena.com
---
drivers/cxl/core/region.c | 32 ++++++++++++++++++++++++--------
1 file changed, 24 insertions(+), 8 deletions(-)
diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c index 237172b33f55..f9538ac98a1f 100644
--- a/drivers/cxl/core/region.c
+++ b/drivers/cxl/core/region.c
@@ -4437,7 +4437,8 @@ static int first_mapped_decoder(struct device *dev, const void *data)
* Runs in cxl_mem_probe context after successful endpoint probe, assumes the
* simple case of single mapped decoder per memdev.
*/
-int cxl_memdev_attach_region(struct cxl_memdev *cxlmd)
+static int __cxl_memdev_attach_region(struct cxl_memdev *cxlmd,
+ struct cxl_region **unwind)
{
struct cxl_attach_region *attach =
container_of(cxlmd->attach, typeof(*attach), attach); @@ -4446,10 +4447,6 @@ int cxl_memdev_attach_region(struct cxl_memdev *cxlmd)
struct cxl_region *cxlr;
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);
@@ -4484,10 +4481,11 @@ int cxl_memdev_attach_region(struct cxl_memdev *cxlmd)
/* Only teardown regions that pass validation, ignore the rest */
get_device(&cxlr->dev);
- rc = devm_add_action_or_reset(&endpoint->dev,
- endpoint_unregister_region, cxlr);
- if (rc)
+ rc = devm_add_action(&endpoint->dev, endpoint_unregister_region, cxlr);
+ if (rc) {
+ *unwind = cxlr;
return rc;
+ }
attach->hpa_range = (struct range) {
.start = cxlr->params.res->start,
@@ -4495,6 +4493,24 @@ int cxl_memdev_attach_region(struct cxl_memdev *cxlmd)
};
return 0;
}
+
+int cxl_memdev_attach_region(struct cxl_memdev *cxlmd) {
+ struct cxl_port *endpoint = cxlmd->endpoint;
+ struct cxl_region *unwind = NULL;
+ int rc;
+
+ /* hold endpoint lock to setup autoremove of the region */
+ guard(device)(&endpoint->dev);
+ if (!endpoint->dev.driver)
+ return -ENXIO;
+
+ /* Detaching the region takes cxl_rwsem.region, so unwind without it */
+ rc = __cxl_memdev_attach_region(cxlmd, &unwind);
+ if (unwind)
+ endpoint_unregister_region(unwind);
+ return rc;
+}
EXPORT_SYMBOL_FOR_MODULES(cxl_memdev_attach_region, "cxl_mem");
/*
---
base-commit: 32af7819352bae9c690ebe277a44eceb501d5c68
change-id: 20261113-junhomax-fix-cxl-attach-region-deadlock-fc0a425f7265
Best regards,
--
Junho Cha <junho.cha@xcena.com>
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH v2] cxl/region: Fix self-deadlock when cxl_memdev_attach_region() fails
2026-11-19 1:44 [PATCH v2] cxl/region: Fix self-deadlock when cxl_memdev_attach_region() fails Junho Cha via B4 Relay
2026-12-03 0:52 ` 차준호
@ 2026-12-03 20:50 ` Alison Schofield
1 sibling, 0 replies; 3+ messages in thread
From: Alison Schofield @ 2026-12-03 20:50 UTC (permalink / raw)
To: junho.cha
Cc: Dave Jiang, linux-cxl, Davidlohr Bueso, Jonathan Cameron,
Vishal Verma, Ira Weiny, Li Ming, Ben Cheatham, Alejandro Lucero,
Richard Cheng, linux-kernel, Sashiko
On Thu, Nov 19, 2026 at 10:44:36AM +0900, Junho Cha via B4 Relay wrote:
> 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.
>
Hi Junho,
The code changes look functional, but I'll ask for changes to this
commit log and the variable naming inline below.
Please rewrite the commit log as a concise kernel-style summary.
Replace the call trace and the surrounding walkthrough with a short
paragraph that leads with the bug, like "a devres allocation failure
during cxl_mem probe can deadlock the probing task because cleanup
tries to take the region rwsem for write while that task holds it
for read."
The wait is killable, so "never makes progress" is too absolute.
A fatal signal can interrupt the detach and leave the decoder
attached to an unregistered region. Keep the lockdep inversion
to one sentence.
> Register the action with devm_add_action() instead, still under both
> rwsems, and if that fails, run it only after the rwsems are dropped.
> The endpoint device lock stays held throughout, as before.
>
Summarize the locking rationale. Something like -
Keep both rwsems held through action registration so the region
cannot be detached between validation and registration. If
registration fails, release both rwsems before unregistering the
region to avoid the self-deadlock and the lock-order inversion.
Keep the endpoint device lock held throughout.
> 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 task hangs
> in cxl_decoder_detach(). With it, the cxl_mem probe fails with
> -ENOMEM, the region is unregistered, and lockdep stays quiet.
Keep the fault-injection setup and close with the result: probe
returned -ENOMEM, the region was unregistered, and lockdep reported
no inversion.
See renaming asks inline below...
>
> Fixes: d8dcb0b74b04 ("cxl/region: Introduce devm_cxl_probe_mem()")
> Reported-by: Sashiko <sashiko-bot@kernel.org>
> Link: https://lore.kernel.org/all/20260805080537.7489E1F000E9@smtp.kernel.org/
> Assisted-by: LLM
> 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/
> ---
> Changes in v2:
> - Register the action under both rwsems as before and only run the
> unwind after dropping them. v1 left a window where the region could
> be detached before the action was registered (Ben).
> - Add Reported-by/Link for Sashiko.
> - Drop Alejandro's Reviewed-by, since the fix changed.
> - Link to v1: https://patch.msgid.link/20261117-junhomax-fix-cxl-attach-region-deadlock-v1-1-f373a135edd2@xcena.com
> ---
> drivers/cxl/core/region.c | 32 ++++++++++++++++++++++++--------
> 1 file changed, 24 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
> index 237172b33f55..f9538ac98a1f 100644
> --- a/drivers/cxl/core/region.c
> +++ b/drivers/cxl/core/region.c
> @@ -4437,7 +4437,8 @@ static int first_mapped_decoder(struct device *dev, const void *data)
> * Runs in cxl_mem_probe context after successful endpoint probe, assumes the
> * simple case of single mapped decoder per memdev.
> */
> -int cxl_memdev_attach_region(struct cxl_memdev *cxlmd)
> +static int __cxl_memdev_attach_region(struct cxl_memdev *cxlmd,
> + struct cxl_region **unwind)
Please call this out-parameter region_out. "Unwind" describes the
error-handling operation. This parameter gives the caller the
region to unregister.
> {
> struct cxl_attach_region *attach =
> container_of(cxlmd->attach, typeof(*attach), attach);
> @@ -4446,10 +4447,6 @@ int cxl_memdev_attach_region(struct cxl_memdev *cxlmd)
> struct cxl_region *cxlr;
> 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);
>
> @@ -4484,10 +4481,11 @@ int cxl_memdev_attach_region(struct cxl_memdev *cxlmd)
>
> /* Only teardown regions that pass validation, ignore the rest */
> get_device(&cxlr->dev);
> - rc = devm_add_action_or_reset(&endpoint->dev,
> - endpoint_unregister_region, cxlr);
> - if (rc)
> + rc = devm_add_action(&endpoint->dev, endpoint_unregister_region, cxlr);
> + if (rc) {
> + *unwind = cxlr;
> return rc;
> + }
>
> attach->hpa_range = (struct range) {
> .start = cxlr->params.res->start,
> @@ -4495,6 +4493,24 @@ int cxl_memdev_attach_region(struct cxl_memdev *cxlmd)
> };
> return 0;
> }
> +
> +int cxl_memdev_attach_region(struct cxl_memdev *cxlmd)
> +{
> + struct cxl_port *endpoint = cxlmd->endpoint;
> + struct cxl_region *unwind = NULL;
Please call this pointer cxlr. The call to endpoint_unregister_region(cxlr)
already shows what the caller does with it.
> + int rc;
> +
> + /* hold endpoint lock to setup autoremove of the region */
> + guard(device)(&endpoint->dev);
> + if (!endpoint->dev.driver)
> + return -ENXIO;
> +
> + /* Detaching the region takes cxl_rwsem.region, so unwind without it */
I'll also suggest dropping this comment. The helper's scoped guards show where the
rwsems are released, and the call to endpoint_unregister_region() shows what happens
afterward. So - the code is self-documenting. "Unwind without it" just restates that
ordering and does so less clearly IMO.
> + rc = __cxl_memdev_attach_region(cxlmd, &unwind);
> + if (unwind)
> + endpoint_unregister_region(unwind);
> + return rc;
> +}
> EXPORT_SYMBOL_FOR_MODULES(cxl_memdev_attach_region, "cxl_mem");
>
> /*
>
> ---
> base-commit: 32af7819352bae9c690ebe277a44eceb501d5c68
> change-id: 20261113-junhomax-fix-cxl-attach-region-deadlock-fc0a425f7265
>
> Best regards,
> --
> Junho Cha <junho.cha@xcena.com>
>
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-03 20:51 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 1:44 [PATCH v2] cxl/region: Fix self-deadlock when cxl_memdev_attach_region() fails Junho Cha via B4 Relay
2026-12-03 0:52 ` 차준호
2026-12-03 20:50 ` Alison Schofield
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®