mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Cheatham, Benjamin" <benjamin.cheatham@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 15:49:39 -0600	[thread overview]
Message-ID: <23185fba-5905-4671-8d94-8ae10c78077e@amd.com> (raw)
In-Reply-To: <20261117-junhomax-fix-cxl-attach-region-deadlock-v1-1-f373a135edd2@xcena.com>

On 11/16/2026 8:59 PM, 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.

I don't think the lockdep dependency is related to this, it's in a completely
different code path. That said, it may be fixable by converting the guard() on
the cxlrd->regions_lock in cxl_add_to_region() to a scoped_guard().
[snip]

> -	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;
>  

I think this could cause an issue if you have multiple type 2 cards in the same region. Imagine this scenario:

1. 2 type-2 cards both complete the critical section guarded by the cxl_rwsem locks above and have cxlr set to
the same region
2. The first card fails to add the action for some reason and runs cleanup before the other card gets to enter
devm_add_action_or_reset()
	- At this point, the region is deleted and all the decoders detached, etc.
3. The second card then successfully adds the action and passes back an HPA range attached to a dead region

Maybe you can get past this by keeping just the cxl_rwsem.dpa lock through the end of the function? I don't
know if that's too hacky, but I don't recall seeing the dpa lock taken by anything after endpoint_unregister_region()
gets called so it may work.

Thanks,
Ben


      parent reply	other threads:[~2026-11-17 21:49 UTC|newest]

Thread overview: 3+ 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
2026-11-17 21:49 ` Cheatham, Benjamin [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=23185fba-5905-4671-8d94-8ae10c78077e@amd.com \
    --to=benjamin.cheatham@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®