Re: [PATCH] cxl/region: Fix self-deadlock when cxl_memdev_attach_region() fails
From: Cheatham, Benjamin
Date: Tue Nov 17 2026 - 16:49:56 EST
On 11/16/2026 8:59 PM, Junho Cha via B4 Relay wrote:
> From: Junho Cha <junho.cha@xxxxxxxxx>
>
> 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