Linux CXL
 help / color / mirror / Atom feed
* [PATCH v3] cxl/memdev: fix deadlock in cxl_memdev_autoremove() on attach failure
@ 2026-02-11 19:22 Gregory Price
  2026-02-11 20:59 ` Davidlohr Bueso
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Gregory Price @ 2026-02-11 19:22 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()

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).

Add cxl_memdev_attach_failed() to set the scope of the check correctly.

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 | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/drivers/cxl/core/memdev.c b/drivers/cxl/core/memdev.c
index af3d0cc65138..25ca4443e4f7 100644
--- a/drivers/cxl/core/memdev.c
+++ b/drivers/cxl/core/memdev.c
@@ -1089,10 +1089,8 @@ static int cxlmd_add(struct cxl_memdev *cxlmd, struct cxl_dev_state *cxlds)
 DEFINE_FREE(put_cxlmd, struct cxl_memdev *,
 	    if (!IS_ERR_OR_NULL(_T)) put_device(&_T->dev))
 
-static struct cxl_memdev *cxl_memdev_autoremove(struct cxl_memdev *cxlmd)
+static bool cxl_memdev_attach_failed(struct cxl_memdev *cxlmd)
 {
-	int rc;
-
 	/*
 	 * If @attach is provided fail if the driver is not attached upon
 	 * return. Note that failure here could be the result of a race to
@@ -1100,7 +1098,14 @@ static struct cxl_memdev *cxl_memdev_autoremove(struct cxl_memdev *cxlmd)
 	 * succeeded and then cxl_mem unbound before the lock is acquired.
 	 */
 	guard(device)(&cxlmd->dev);
-	if (cxlmd->attach && !cxlmd->dev.driver) {
+	return (cxlmd->attach && !cxlmd->dev.driver);
+}
+
+static struct cxl_memdev *cxl_memdev_autoremove(struct cxl_memdev *cxlmd)
+{
+	int rc;
+
+	if (cxl_memdev_attach_failed(cxlmd)) {
 		cxl_memdev_unregister(cxlmd);
 		return ERR_PTR(-ENXIO);
 	}
-- 
2.53.0


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

* Re: [PATCH v3] cxl/memdev: fix deadlock in cxl_memdev_autoremove() on attach failure
  2026-02-11 19:22 [PATCH v3] cxl/memdev: fix deadlock in cxl_memdev_autoremove() on attach failure Gregory Price
@ 2026-02-11 20:59 ` Davidlohr Bueso
  2026-02-13  3:16 ` dan.j.williams
  2026-02-23 22:33 ` Dave Jiang
  2 siblings, 0 replies; 5+ messages in thread
From: Davidlohr Bueso @ 2026-02-11 20:59 UTC (permalink / raw)
  To: Gregory Price
  Cc: linux-cxl, linux-kernel, kernel-team, jonathan.cameron,
	dave.jiang, alison.schofield, vishal.l.verma, ira.weiny,
	dan.j.williams

On Wed, 11 Feb 2026, 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()
>
>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).
>
>Add cxl_memdev_attach_failed() to set the scope of the check correctly.

I preferred the "if (!cxl_memdev_did_attach())" but the below still
reads nicely.

Reviewed-by: Davidlohr Bueso <dave@stgolabs.net>

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

* Re: [PATCH v3] cxl/memdev: fix deadlock in cxl_memdev_autoremove() on attach failure
  2026-02-11 19:22 [PATCH v3] cxl/memdev: fix deadlock in cxl_memdev_autoremove() on attach failure Gregory Price
  2026-02-11 20:59 ` Davidlohr Bueso
@ 2026-02-13  3:16 ` dan.j.williams
  2026-02-13  5:19   ` Gregory Price
  2026-02-23 22:33 ` Dave Jiang
  2 siblings, 1 reply; 5+ messages in thread
From: dan.j.williams @ 2026-02-13  3:16 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()
> 
> 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).
> 
> Add cxl_memdev_attach_failed() to set the scope of the check correctly.
> 
> Fixes: 29317f8dc6ed ("cxl/mem: Introduce cxl_memdev_attach for CXL-dependent operation")
> Signed-off-by: Gregory Price <gourry@gourry.net>

Looks good,

Reviewed-by: Dan Williams <dan.j.williams@intel.com>

This was a kreview find? Would it make sense to do something like
"Reported-by: kreview-<git sha>"?

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

* Re: [PATCH v3] cxl/memdev: fix deadlock in cxl_memdev_autoremove() on attach failure
  2026-02-13  3:16 ` dan.j.williams
@ 2026-02-13  5:19   ` Gregory Price
  0 siblings, 0 replies; 5+ messages in thread
From: Gregory Price @ 2026-02-13  5:19 UTC (permalink / raw)
  To: dan.j.williams
  Cc: linux-cxl, linux-kernel, kernel-team, dave, jonathan.cameron,
	dave.jiang, alison.schofield, vishal.l.verma, ira.weiny

On Thu, Feb 12, 2026 at 07:16:41PM -0800, dan.j.williams@intel.com wrote:
> 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()
> > 
> > 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).
> > 
> > Add cxl_memdev_attach_failed() to set the scope of the check correctly.
> > 
> > Fixes: 29317f8dc6ed ("cxl/mem: Introduce cxl_memdev_attach for CXL-dependent operation")
> > Signed-off-by: Gregory Price <gourry@gourry.net>
> 
> Looks good,
> 
> Reviewed-by: Dan Williams <dan.j.williams@intel.com>
> 
> This was a kreview find? Would it make sense to do something like
> "Reported-by: kreview-<git sha>"?

Technically it's a combination of Claude using kreview, but I think this
is reasonable since Claude falls over on some of this without the
prompts.

If that's something we want:

Reported-by: kreview-c94b85d6d2

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

* Re: [PATCH v3] cxl/memdev: fix deadlock in cxl_memdev_autoremove() on attach failure
  2026-02-11 19:22 [PATCH v3] cxl/memdev: fix deadlock in cxl_memdev_autoremove() on attach failure Gregory Price
  2026-02-11 20:59 ` Davidlohr Bueso
  2026-02-13  3:16 ` dan.j.williams
@ 2026-02-23 22:33 ` Dave Jiang
  2 siblings, 0 replies; 5+ messages in thread
From: Dave Jiang @ 2026-02-23 22:33 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/11/26 12:22 PM, 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()
> 
> 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).
> 
> Add cxl_memdev_attach_failed() to set the scope of the check correctly.
> 
> Fixes: 29317f8dc6ed ("cxl/mem: Introduce cxl_memdev_attach for CXL-dependent operation")
> Signed-off-by: Gregory Price <gourry@gourry.net>

Applied to cxl/fixes
318c58852e68

> ---
>  drivers/cxl/core/memdev.c | 13 +++++++++----
>  1 file changed, 9 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/cxl/core/memdev.c b/drivers/cxl/core/memdev.c
> index af3d0cc65138..25ca4443e4f7 100644
> --- a/drivers/cxl/core/memdev.c
> +++ b/drivers/cxl/core/memdev.c
> @@ -1089,10 +1089,8 @@ static int cxlmd_add(struct cxl_memdev *cxlmd, struct cxl_dev_state *cxlds)
>  DEFINE_FREE(put_cxlmd, struct cxl_memdev *,
>  	    if (!IS_ERR_OR_NULL(_T)) put_device(&_T->dev))
>  
> -static struct cxl_memdev *cxl_memdev_autoremove(struct cxl_memdev *cxlmd)
> +static bool cxl_memdev_attach_failed(struct cxl_memdev *cxlmd)
>  {
> -	int rc;
> -
>  	/*
>  	 * If @attach is provided fail if the driver is not attached upon
>  	 * return. Note that failure here could be the result of a race to
> @@ -1100,7 +1098,14 @@ static struct cxl_memdev *cxl_memdev_autoremove(struct cxl_memdev *cxlmd)
>  	 * succeeded and then cxl_mem unbound before the lock is acquired.
>  	 */
>  	guard(device)(&cxlmd->dev);
> -	if (cxlmd->attach && !cxlmd->dev.driver) {
> +	return (cxlmd->attach && !cxlmd->dev.driver);
> +}
> +
> +static struct cxl_memdev *cxl_memdev_autoremove(struct cxl_memdev *cxlmd)
> +{
> +	int rc;
> +
> +	if (cxl_memdev_attach_failed(cxlmd)) {
>  		cxl_memdev_unregister(cxlmd);
>  		return ERR_PTR(-ENXIO);
>  	}


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

end of thread, other threads:[~2026-02-23 22:34 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-11 19:22 [PATCH v3] cxl/memdev: fix deadlock in cxl_memdev_autoremove() on attach failure Gregory Price
2026-02-11 20:59 ` Davidlohr Bueso
2026-02-13  3:16 ` dan.j.williams
2026-02-13  5:19   ` Gregory Price
2026-02-23 22:33 ` Dave Jiang

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