Commit 318c5885 authored by Gregory Price's avatar Gregory Price Committed by Dave Jiang
Browse files

cxl/memdev: fix deadlock in cxl_memdev_autoremove() on attach failure



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.

Reported-by: kreview-c94b85d6d2
Fixes: 29317f8d ("cxl/mem: Introduce cxl_memdev_attach for CXL-dependent operation")
Signed-off-by: default avatarGregory Price <gourry@gourry.net>
Reviewed-by: default avatarDan Williams <dan.j.williams@intel.com>
Reviewed-by: default avatarDavidlohr Bueso <dave@stgolabs.net>
Link: https://patch.msgid.link/20260211192228.2148713-1-gourry@gourry.net


Signed-off-by: default avatarDave Jiang <dave.jiang@intel.com>
parent 6de23f81
Loading
Loading
Loading
Loading
+9 −4
Original line number Diff line number Diff line
@@ -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);
	}