6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Dan Williams [ Upstream commit d91feb88692e81b00cd22f0125cfcd04970b4a0b ] Expand the range lock, rename it "regions_lock", to disable region deletion in the critical period between construct_region() and attach_target(), as well as the period between device_add() and registering the remove actions. Otherwise, userspace can confuse the kernel. It can violate the assumption the region stays registered through the completion of cxl_add_to_region(). It can violate the assumption that devm_add_action_or_reset() is working with a live 'struct cxl_region'. It is ok for the region to disappear outside of those windows as that mirrors device hotplug flows where the proper locks are held. Fixes: a32320b71f08 ("cxl/region: Add region autodiscovery") Signed-off-by: Dan Williams Reviewed-by: Alejandro Lucero Tested-by: ALejandro Lucero Reviewed-by: Dave Jiang Link: https://patch.msgid.link/20260519210158.1499795-2-djbw@kernel.org Signed-off-by: Dave Jiang Signed-off-by: Sasha Levin --- drivers/cxl/core/port.c | 2 +- drivers/cxl/core/region.c | 23 ++++++++++++++++++++--- drivers/cxl/cxl.h | 4 ++-- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/drivers/cxl/core/port.c b/drivers/cxl/core/port.c index f23b5d6cde070..7746e2691802c 100644 --- a/drivers/cxl/core/port.c +++ b/drivers/cxl/core/port.c @@ -1707,7 +1707,7 @@ struct cxl_root_decoder *cxl_root_decoder_alloc(struct cxl_port *port, } cxlrd->calc_hb = calc_hb; - mutex_init(&cxlrd->range_lock); + mutex_init(&cxlrd->regions_lock); cxld = &cxlsd->cxld; cxld->dev.type = &cxl_decoder_root_type; diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c index 97e4a4755ab2f..5fc2a6a50ae94 100644 --- a/drivers/cxl/core/region.c +++ b/drivers/cxl/core/region.c @@ -2349,7 +2349,12 @@ static ssize_t create_pmem_region_store(struct device *dev, if (rc != 1) return -EINVAL; + rc = mutex_lock_interruptible(&cxlrd->regions_lock); + if (rc) + return rc; + cxlr = __create_region(cxlrd, CXL_DECODER_PMEM, id); + mutex_unlock(&cxlrd->regions_lock); if (IS_ERR(cxlr)) return PTR_ERR(cxlr); @@ -2369,7 +2374,12 @@ static ssize_t create_ram_region_store(struct device *dev, if (rc != 1) return -EINVAL; + rc = mutex_lock_interruptible(&cxlrd->regions_lock); + if (rc) + return rc; + cxlr = __create_region(cxlrd, CXL_DECODER_RAM, id); + mutex_unlock(&cxlrd->regions_lock); if (IS_ERR(cxlr)) return PTR_ERR(cxlr); @@ -2417,13 +2427,21 @@ static ssize_t delete_region_store(struct device *dev, struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev); struct cxl_port *port = to_cxl_port(dev->parent); struct cxl_region *cxlr; + int rc; + + rc = mutex_lock_interruptible(&cxlrd->regions_lock); + if (rc) + return rc; cxlr = cxl_find_region_by_name(cxlrd, buf); - if (IS_ERR(cxlr)) + if (IS_ERR(cxlr)) { + mutex_unlock(&cxlrd->regions_lock); return PTR_ERR(cxlr); + } devm_release_action(port->uport_dev, unregister_region, cxlr); put_device(&cxlr->dev); + mutex_unlock(&cxlrd->regions_lock); return len; } @@ -3047,7 +3065,7 @@ int cxl_add_to_region(struct cxl_port *root, struct cxl_endpoint_decoder *cxled) * Ensure that if multiple threads race to construct_region() for @hpa * one does the construction and the others add to that. */ - mutex_lock(&cxlrd->range_lock); + guard(mutex)(&cxlrd->regions_lock); region_dev = device_find_child(&cxlrd->cxlsd.cxld.dev, hpa, match_region_by_range); if (!region_dev) { @@ -3055,7 +3073,6 @@ int cxl_add_to_region(struct cxl_port *root, struct cxl_endpoint_decoder *cxled) region_dev = &cxlr->dev; } else cxlr = to_cxl_region(region_dev); - mutex_unlock(&cxlrd->range_lock); rc = PTR_ERR_OR_ZERO(cxlr); if (rc) diff --git a/drivers/cxl/cxl.h b/drivers/cxl/cxl.h index 59e963e39d31b..f3260ca17522a 100644 --- a/drivers/cxl/cxl.h +++ b/drivers/cxl/cxl.h @@ -431,7 +431,7 @@ typedef struct cxl_dport *(*cxl_calc_hb_fn)(struct cxl_root_decoder *cxlrd, * @region_id: region id for next region provisioning event * @calc_hb: which host bridge covers the n'th position by granularity * @platform_data: platform specific configuration data - * @range_lock: sync region autodiscovery by address range + * @regions_lock: sync region discovery, construction, and deletion * @cxlsd: base cxl switch decoder */ struct cxl_root_decoder { @@ -439,7 +439,7 @@ struct cxl_root_decoder { atomic_t region_id; cxl_calc_hb_fn calc_hb; void *platform_data; - struct mutex range_lock; + struct mutex regions_lock; struct cxl_switch_decoder cxlsd; }; -- 2.53.0