* [PATCH] cxl/memdev: fix deadlock in cxl_memdev_autoremove() on attach failure
@ 2026-02-10 15:43 Gregory Price
2026-02-10 16:27 ` Dave Jiang
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Gregory Price @ 2026-02-10 15:43 UTC (permalink / raw)
To: linux-cxl
Cc: linux-kernel, kernel-team, dave, jonathan.cameron, dave.jiang,
alison.schofield, vishal.l.verma, ira.weiny, dan.j.williams
cxl_memdev_autoremove() takes device_lock(&cxlmd->dev) via guard(device)
and then calls cxl_memdev_unregister() when the attach callback was
provided but cxl_mem_probe() failed to bind.
cxl_memdev_unregister() calls
cdev_device_del()
device_del()
bus_remove_device()
device_release_driver()
which also takes device_lock(), deadlocking the calling thread.
This path is reached when a driver uses the @attach parameter to
devm_cxl_add_memdev() and the CXL topology fails to enumerate (e.g.
DVSEC range registers decode outside platform-defined CXL ranges,
causing the endpoint port probe to fail).
Fix by using scoped_guard() and breaking out of the guard scope before
calling cxl_memdev_unregister(), so device_lock() is released first.
Fixes: 29317f8dc6ed ("cxl/mem: Introduce cxl_memdev_attach for CXL-dependent operation")
Signed-off-by: Gregory Price <gourry@gourry.net>
---
drivers/cxl/core/memdev.c | 25 ++++++++++++++-----------
1 file changed, 14 insertions(+), 11 deletions(-)
diff --git a/drivers/cxl/core/memdev.c b/drivers/cxl/core/memdev.c
index af3d0cc65138..c0de767b24fb 100644
--- a/drivers/cxl/core/memdev.c
+++ b/drivers/cxl/core/memdev.c
@@ -1098,19 +1098,22 @@ static struct cxl_memdev *cxl_memdev_autoremove(struct cxl_memdev *cxlmd)
* return. Note that failure here could be the result of a race to
* teardown the CXL port topology. I.e. cxl_mem_probe() could have
* succeeded and then cxl_mem unbound before the lock is acquired.
+ *
+ * Check under device_lock but unregister outside of it, as
+ * cxl_memdev_unregister() will also take the device lock.
*/
- guard(device)(&cxlmd->dev);
- if (cxlmd->attach && !cxlmd->dev.driver) {
- cxl_memdev_unregister(cxlmd);
- return ERR_PTR(-ENXIO);
+ scoped_guard(device, &cxlmd->dev) {
+ if (cxlmd->attach && !cxlmd->dev.driver)
+ break;
+
+ rc = devm_add_action_or_reset(cxlmd->cxlds->dev,
+ cxl_memdev_unregister, cxlmd);
+ if (rc)
+ return ERR_PTR(rc);
+ return cxlmd;
}
-
- rc = devm_add_action_or_reset(cxlmd->cxlds->dev, cxl_memdev_unregister,
- cxlmd);
- if (rc)
- return ERR_PTR(rc);
-
- return cxlmd;
+ cxl_memdev_unregister(cxlmd);
+ return ERR_PTR(-ENXIO);
}
/*
--
2.53.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH] cxl/memdev: fix deadlock in cxl_memdev_autoremove() on attach failure
2026-02-10 15:43 [PATCH] cxl/memdev: fix deadlock in cxl_memdev_autoremove() on attach failure Gregory Price
@ 2026-02-10 16:27 ` Dave Jiang
2026-02-10 19:44 ` Ira Weiny
2026-02-10 23:18 ` dan.j.williams
2 siblings, 0 replies; 7+ messages in thread
From: Dave Jiang @ 2026-02-10 16:27 UTC (permalink / raw)
To: Gregory Price, linux-cxl
Cc: linux-kernel, kernel-team, dave, jonathan.cameron,
alison.schofield, vishal.l.verma, ira.weiny, dan.j.williams
On 2/10/26 8:43 AM, Gregory Price wrote:
> cxl_memdev_autoremove() takes device_lock(&cxlmd->dev) via guard(device)
> and then calls cxl_memdev_unregister() when the attach callback was
> provided but cxl_mem_probe() failed to bind.
>
> cxl_memdev_unregister() calls
> cdev_device_del()
> device_del()
> bus_remove_device()
> device_release_driver()
>
> which also takes device_lock(), deadlocking the calling thread.
>
> This path is reached when a driver uses the @attach parameter to
> devm_cxl_add_memdev() and the CXL topology fails to enumerate (e.g.
> DVSEC range registers decode outside platform-defined CXL ranges,
> causing the endpoint port probe to fail).
>
> Fix by using scoped_guard() and breaking out of the guard scope before
> calling cxl_memdev_unregister(), so device_lock() is released first.
>
> Fixes: 29317f8dc6ed ("cxl/mem: Introduce cxl_memdev_attach for CXL-dependent operation")
> Signed-off-by: Gregory Price <gourry@gourry.net>
Reivewed-by: Dave Jiang <dave.jiang@intel.com>
> ---
> drivers/cxl/core/memdev.c | 25 ++++++++++++++-----------
> 1 file changed, 14 insertions(+), 11 deletions(-)
>
> diff --git a/drivers/cxl/core/memdev.c b/drivers/cxl/core/memdev.c
> index af3d0cc65138..c0de767b24fb 100644
> --- a/drivers/cxl/core/memdev.c
> +++ b/drivers/cxl/core/memdev.c
> @@ -1098,19 +1098,22 @@ static struct cxl_memdev *cxl_memdev_autoremove(struct cxl_memdev *cxlmd)
> * return. Note that failure here could be the result of a race to
> * teardown the CXL port topology. I.e. cxl_mem_probe() could have
> * succeeded and then cxl_mem unbound before the lock is acquired.
> + *
> + * Check under device_lock but unregister outside of it, as
> + * cxl_memdev_unregister() will also take the device lock.
> */
> - guard(device)(&cxlmd->dev);
> - if (cxlmd->attach && !cxlmd->dev.driver) {
> - cxl_memdev_unregister(cxlmd);
> - return ERR_PTR(-ENXIO);
> + scoped_guard(device, &cxlmd->dev) {
> + if (cxlmd->attach && !cxlmd->dev.driver)
> + break;
> +
> + rc = devm_add_action_or_reset(cxlmd->cxlds->dev,
> + cxl_memdev_unregister, cxlmd);
> + if (rc)
> + return ERR_PTR(rc);
> + return cxlmd;
> }
> -
> - rc = devm_add_action_or_reset(cxlmd->cxlds->dev, cxl_memdev_unregister,
> - cxlmd);
> - if (rc)
> - return ERR_PTR(rc);
> -
> - return cxlmd;
> + cxl_memdev_unregister(cxlmd);
> + return ERR_PTR(-ENXIO);
> }
>
> /*
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] cxl/memdev: fix deadlock in cxl_memdev_autoremove() on attach failure
2026-02-10 15:43 [PATCH] cxl/memdev: fix deadlock in cxl_memdev_autoremove() on attach failure Gregory Price
2026-02-10 16:27 ` Dave Jiang
@ 2026-02-10 19:44 ` Ira Weiny
2026-02-10 22:46 ` Gregory Price
2026-02-10 23:18 ` dan.j.williams
2 siblings, 1 reply; 7+ messages in thread
From: Ira Weiny @ 2026-02-10 19:44 UTC (permalink / raw)
To: Gregory Price, linux-cxl
Cc: linux-kernel, kernel-team, dave, jonathan.cameron, dave.jiang,
alison.schofield, vishal.l.verma, ira.weiny, dan.j.williams
Gregory Price wrote:
> cxl_memdev_autoremove() takes device_lock(&cxlmd->dev) via guard(device)
> and then calls cxl_memdev_unregister() when the attach callback was
> provided but cxl_mem_probe() failed to bind.
>
> cxl_memdev_unregister() calls
> cdev_device_del()
> device_del()
> bus_remove_device()
> device_release_driver()
>
> which also takes device_lock(), deadlocking the calling thread.
>
> This path is reached when a driver uses the @attach parameter to
> devm_cxl_add_memdev() and the CXL topology fails to enumerate (e.g.
> DVSEC range registers decode outside platform-defined CXL ranges,
> causing the endpoint port probe to fail).
>
> Fix by using scoped_guard() and breaking out of the guard scope before
> calling cxl_memdev_unregister(), so device_lock() is released first.
>
> Fixes: 29317f8dc6ed ("cxl/mem: Introduce cxl_memdev_attach for CXL-dependent operation")
> Signed-off-by: Gregory Price <gourry@gourry.net>
> ---
> drivers/cxl/core/memdev.c | 25 ++++++++++++++-----------
> 1 file changed, 14 insertions(+), 11 deletions(-)
>
> diff --git a/drivers/cxl/core/memdev.c b/drivers/cxl/core/memdev.c
> index af3d0cc65138..c0de767b24fb 100644
> --- a/drivers/cxl/core/memdev.c
> +++ b/drivers/cxl/core/memdev.c
> @@ -1098,19 +1098,22 @@ static struct cxl_memdev *cxl_memdev_autoremove(struct cxl_memdev *cxlmd)
> * return. Note that failure here could be the result of a race to
> * teardown the CXL port topology. I.e. cxl_mem_probe() could have
> * succeeded and then cxl_mem unbound before the lock is acquired.
> + *
> + * Check under device_lock but unregister outside of it, as
> + * cxl_memdev_unregister() will also take the device lock.
> */
> - guard(device)(&cxlmd->dev);
> - if (cxlmd->attach && !cxlmd->dev.driver) {
> - cxl_memdev_unregister(cxlmd);
> - return ERR_PTR(-ENXIO);
> + scoped_guard(device, &cxlmd->dev) {
> + if (cxlmd->attach && !cxlmd->dev.driver)
> + break;
> +
> + rc = devm_add_action_or_reset(cxlmd->cxlds->dev,
> + cxl_memdev_unregister, cxlmd);
This kind of threw me... Won't this deadlock if
devm_add_action_or_reset() fails as well?
Need to use devm_add_action() and drop out of the guard on failure.
Ira
> + if (rc)
> + return ERR_PTR(rc);
> + return cxlmd;
> }
> -
> - rc = devm_add_action_or_reset(cxlmd->cxlds->dev, cxl_memdev_unregister,
> - cxlmd);
> - if (rc)
> - return ERR_PTR(rc);
> -
> - return cxlmd;
> + cxl_memdev_unregister(cxlmd);
> + return ERR_PTR(-ENXIO);
> }
>
> /*
> --
> 2.53.0
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] cxl/memdev: fix deadlock in cxl_memdev_autoremove() on attach failure
2026-02-10 19:44 ` Ira Weiny
@ 2026-02-10 22:46 ` Gregory Price
2026-02-10 23:20 ` Dave Jiang
0 siblings, 1 reply; 7+ messages in thread
From: Gregory Price @ 2026-02-10 22:46 UTC (permalink / raw)
To: Ira Weiny
Cc: linux-cxl, linux-kernel, kernel-team, dave, jonathan.cameron,
dave.jiang, alison.schofield, vishal.l.verma, dan.j.williams
On Tue, Feb 10, 2026 at 01:44:06PM -0600, Ira Weiny wrote:
> Gregory Price wrote:
> >
> > diff --git a/drivers/cxl/core/memdev.c b/drivers/cxl/core/memdev.c
> > index af3d0cc65138..c0de767b24fb 100644
> > --- a/drivers/cxl/core/memdev.c
> > +++ b/drivers/cxl/core/memdev.c
> > @@ -1098,19 +1098,22 @@ static struct cxl_memdev *cxl_memdev_autoremove(struct cxl_memdev *cxlmd)
> > * return. Note that failure here could be the result of a race to
> > * teardown the CXL port topology. I.e. cxl_mem_probe() could have
> > * succeeded and then cxl_mem unbound before the lock is acquired.
> > + *
> > + * Check under device_lock but unregister outside of it, as
> > + * cxl_memdev_unregister() will also take the device lock.
> > */
> > - guard(device)(&cxlmd->dev);
> > - if (cxlmd->attach && !cxlmd->dev.driver) {
> > - cxl_memdev_unregister(cxlmd);
> > - return ERR_PTR(-ENXIO);
> > + scoped_guard(device, &cxlmd->dev) {
> > + if (cxlmd->attach && !cxlmd->dev.driver)
> > + break;
> > +
> > + rc = devm_add_action_or_reset(cxlmd->cxlds->dev,
> > + cxl_memdev_unregister, cxlmd);
>
> This kind of threw me... Won't this deadlock if
> devm_add_action_or_reset() fails as well?
>
> Need to use devm_add_action() and drop out of the guard on failure.
>
lol i pointed out that this patch was a claude recommendation on the
initial report - didn't look to hard because it fixed this specific
deadlock with:
if (cxlmd->attach && !cxlmd->dev.driver)
break;
so yeah, easy enough to fixup.
I can either v2 or Dave if you want to just make the oneline change
before pull let me know
~Gregory
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] cxl/memdev: fix deadlock in cxl_memdev_autoremove() on attach failure
2026-02-10 15:43 [PATCH] cxl/memdev: fix deadlock in cxl_memdev_autoremove() on attach failure Gregory Price
2026-02-10 16:27 ` Dave Jiang
2026-02-10 19:44 ` Ira Weiny
@ 2026-02-10 23:18 ` dan.j.williams
2 siblings, 0 replies; 7+ messages in thread
From: dan.j.williams @ 2026-02-10 23:18 UTC (permalink / raw)
To: Gregory Price, linux-cxl
Cc: linux-kernel, kernel-team, dave, jonathan.cameron, dave.jiang,
alison.schofield, vishal.l.verma, ira.weiny, dan.j.williams
Gregory Price wrote:
> cxl_memdev_autoremove() takes device_lock(&cxlmd->dev) via guard(device)
> and then calls cxl_memdev_unregister() when the attach callback was
> provided but cxl_mem_probe() failed to bind.
>
> cxl_memdev_unregister() calls
> cdev_device_del()
> device_del()
> bus_remove_device()
> device_release_driver()
>
> which also takes device_lock(), deadlocking the calling thread.
>
> This path is reached when a driver uses the @attach parameter to
> devm_cxl_add_memdev() and the CXL topology fails to enumerate (e.g.
> DVSEC range registers decode outside platform-defined CXL ranges,
> causing the endpoint port probe to fail).
>
> Fix by using scoped_guard() and breaking out of the guard scope before
> calling cxl_memdev_unregister(), so device_lock() is released first.
Whoops, yes, good catch, but...
>
> Fixes: 29317f8dc6ed ("cxl/mem: Introduce cxl_memdev_attach for CXL-dependent operation")
> Signed-off-by: Gregory Price <gourry@gourry.net>
> ---
> drivers/cxl/core/memdev.c | 25 ++++++++++++++-----------
> 1 file changed, 14 insertions(+), 11 deletions(-)
>
> diff --git a/drivers/cxl/core/memdev.c b/drivers/cxl/core/memdev.c
> index af3d0cc65138..c0de767b24fb 100644
> --- a/drivers/cxl/core/memdev.c
> +++ b/drivers/cxl/core/memdev.c
> @@ -1098,19 +1098,22 @@ static struct cxl_memdev *cxl_memdev_autoremove(struct cxl_memdev *cxlmd)
> * return. Note that failure here could be the result of a race to
> * teardown the CXL port topology. I.e. cxl_mem_probe() could have
> * succeeded and then cxl_mem unbound before the lock is acquired.
> + *
> + * Check under device_lock but unregister outside of it, as
> + * cxl_memdev_unregister() will also take the device lock.
> */
> - guard(device)(&cxlmd->dev);
> - if (cxlmd->attach && !cxlmd->dev.driver) {
> - cxl_memdev_unregister(cxlmd);
> - return ERR_PTR(-ENXIO);
...please just make a helper cxl_memdev_did_attach(), and move the
existing comment there. No need for the addtional comment that just adds
narration.
> + scoped_guard(device, &cxlmd->dev) {
In general, I feel that scoped_guard() is almost always the wrong choice
for existing code and a helper with plain guard() is preferred.
> + if (cxlmd->attach && !cxlmd->dev.driver)
> + break;
> +
> + rc = devm_add_action_or_reset(cxlmd->cxlds->dev,
> + cxl_memdev_unregister, cxlmd);
Just for completeness, no need to hold the lock over this. The helper
can just check ->attach and ->dev.driver.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] cxl/memdev: fix deadlock in cxl_memdev_autoremove() on attach failure
2026-02-10 22:46 ` Gregory Price
@ 2026-02-10 23:20 ` Dave Jiang
2026-02-11 0:05 ` Gregory Price
0 siblings, 1 reply; 7+ messages in thread
From: Dave Jiang @ 2026-02-10 23:20 UTC (permalink / raw)
To: Gregory Price, Ira Weiny
Cc: linux-cxl, linux-kernel, kernel-team, dave, jonathan.cameron,
alison.schofield, vishal.l.verma, dan.j.williams
On 2/10/26 3:46 PM, Gregory Price wrote:
> On Tue, Feb 10, 2026 at 01:44:06PM -0600, Ira Weiny wrote:
>> Gregory Price wrote:
>>>
>>> diff --git a/drivers/cxl/core/memdev.c b/drivers/cxl/core/memdev.c
>>> index af3d0cc65138..c0de767b24fb 100644
>>> --- a/drivers/cxl/core/memdev.c
>>> +++ b/drivers/cxl/core/memdev.c
>>> @@ -1098,19 +1098,22 @@ static struct cxl_memdev *cxl_memdev_autoremove(struct cxl_memdev *cxlmd)
>>> * return. Note that failure here could be the result of a race to
>>> * teardown the CXL port topology. I.e. cxl_mem_probe() could have
>>> * succeeded and then cxl_mem unbound before the lock is acquired.
>>> + *
>>> + * Check under device_lock but unregister outside of it, as
>>> + * cxl_memdev_unregister() will also take the device lock.
>>> */
>>> - guard(device)(&cxlmd->dev);
>>> - if (cxlmd->attach && !cxlmd->dev.driver) {
>>> - cxl_memdev_unregister(cxlmd);
>>> - return ERR_PTR(-ENXIO);
>>> + scoped_guard(device, &cxlmd->dev) {
>>> + if (cxlmd->attach && !cxlmd->dev.driver)
>>> + break;
>>> +
>>> + rc = devm_add_action_or_reset(cxlmd->cxlds->dev,
>>> + cxl_memdev_unregister, cxlmd);
>>
>> This kind of threw me... Won't this deadlock if
>> devm_add_action_or_reset() fails as well?
>>
>> Need to use devm_add_action() and drop out of the guard on failure.
>>
>
> lol i pointed out that this patch was a claude recommendation on the
> initial report - didn't look to hard because it fixed this specific
> deadlock with:
>
> if (cxlmd->attach && !cxlmd->dev.driver)
> break;
>
> so yeah, easy enough to fixup.
>
> I can either v2 or Dave if you want to just make the oneline change
> before pull let me know
With Dan's comments, may as we spin v2. We have time until rc1 releases.
>
> ~Gregory
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] cxl/memdev: fix deadlock in cxl_memdev_autoremove() on attach failure
2026-02-10 23:20 ` Dave Jiang
@ 2026-02-11 0:05 ` Gregory Price
0 siblings, 0 replies; 7+ messages in thread
From: Gregory Price @ 2026-02-11 0:05 UTC (permalink / raw)
To: Dave Jiang
Cc: Ira Weiny, linux-cxl, linux-kernel, kernel-team, dave,
jonathan.cameron, alison.schofield, vishal.l.verma,
dan.j.williams
On Tue, Feb 10, 2026 at 04:20:35PM -0700, Dave Jiang wrote:
>
>
> > I can either v2 or Dave if you want to just make the oneline change
> > before pull let me know
>
> With Dan's comments, may as we spin v2. We have time until rc1 releases.
>
will get it first thing tomorrow
~Gregory
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-02-11 0:05 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-10 15:43 [PATCH] cxl/memdev: fix deadlock in cxl_memdev_autoremove() on attach failure Gregory Price
2026-02-10 16:27 ` Dave Jiang
2026-02-10 19:44 ` Ira Weiny
2026-02-10 22:46 ` Gregory Price
2026-02-10 23:20 ` Dave Jiang
2026-02-11 0:05 ` Gregory Price
2026-02-10 23:18 ` dan.j.williams
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox