mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Lucero Palau, Alejandro" <alejandro.lucero-palau@amd.com>
To: junho.cha@xcena.com, 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>,
	Alejandro Lucero <alucerop@amd.com>,
	Richard Cheng <icheng@nvidia.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] cxl/region: Fix self-deadlock when cxl_memdev_attach_region() fails
Date: Tue, 17 Nov 2026 08:08:00 +0000	[thread overview]
Message-ID: <4b58aead-fbc8-4102-9eff-7e1bc5a8c994@amd.com> (raw)
In-Reply-To: <20261117-junhomax-fix-cxl-attach-region-deadlock-v1-1-f373a135edd2@xcena.com>

Hi Junho,

On 17/11/2026 02:59, 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()


Thanks for giving visibility to this issue.


I have been working on something related to this complex unwinding of 
things, not when an error during initialization happens like this case, 
but under expected unwinding through the different possible cases. Apart 
from being complex and hard to get everything properly sorted out, the 
way the unwinding is programmed is semantically wrong for Type2 and only 
safe due to the restrictions with current Type2 expectations which will 
be overcome soon. I am close to send an RFC about that change implying 
this specific action will be different. I need to study the implications 
for the code I have, but as this will take some discussion and versions 
until is hopefully accepted, your patch is the right thing to do now.


Thanks!


Reviewed-by: Alejandro Lucero <alucerop@amd.com>


>           /* 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>

      reply	other threads:[~2026-11-17  8:09 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17  2:59 Junho Cha via B4 Relay
2026-11-17  8:08 ` Lucero Palau, Alejandro [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4b58aead-fbc8-4102-9eff-7e1bc5a8c994@amd.com \
    --to=alejandro.lucero-palau@amd.com \
    --cc=alison.schofield@intel.com \
    --cc=alucerop@amd.com \
    --cc=dave.jiang@intel.com \
    --cc=dave@stgolabs.net \
    --cc=icheng@nvidia.com \
    --cc=iweiny@kernel.org \
    --cc=jic23@kernel.org \
    --cc=junho.cha@xcena.com \
    --cc=linux-cxl@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ming.li@zohomail.com \
    --cc=vishal.l.verma@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®