Skip to content

Commit 868a8f1

Browse files
Robert Richterdavejiang
authored andcommitted
cxl/region: Factor out code to find a root decoder's region
In function cxl_add_to_region() there is code to determine a root decoder's region. Factor that code out. This is in preparation to further rework and simplify function cxl_add_to_region(). The reference count must be decremented after using the region. cxl_find_region_by_range() is paired with the put_cxl_region cleanup helper that can be used for this. [dj: Fixed up "obj __free(...) = NULL" pattern] Signed-off-by: Robert Richter <rrichter@amd.com> Reviewed-by: Gregory Price <gourry@gourry.net> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Reviewed-by: Dave Jiang <dave.jiang@intel.com> Reviewed-by: Dan Williams <dan.j.williams@intel.com> Reviewed-by: Alison Schofield <alison.schofield@intel.com> Reviewed-by: "Fabio M. De Francesco" <fabio.m.de.francesco@linux.intel.com> Tested-by: Gregory Price <gourry@gourry.net> Acked-by: Dan Williams <dan.j.williams@intel.com> Link: https://patch.msgid.link/20250509150700.2817697-11-rrichter@amd.com Signed-off-by: Dave Jiang <dave.jiang@intel.com>
1 parent 9466ee9 commit 868a8f1

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

drivers/cxl/core/region.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3405,12 +3405,23 @@ static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
34053405
return cxlr;
34063406
}
34073407

3408+
static struct cxl_region *
3409+
cxl_find_region_by_range(struct cxl_root_decoder *cxlrd, struct range *hpa)
3410+
{
3411+
struct device *region_dev;
3412+
3413+
region_dev = device_find_child(&cxlrd->cxlsd.cxld.dev, hpa,
3414+
match_region_by_range);
3415+
if (!region_dev)
3416+
return NULL;
3417+
3418+
return to_cxl_region(region_dev);
3419+
}
3420+
34083421
int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
34093422
{
34103423
struct range *hpa = &cxled->cxld.hpa_range;
3411-
struct device *region_dev;
34123424
struct cxl_region_params *p;
3413-
struct cxl_region *cxlr;
34143425
bool attach = false;
34153426
int rc;
34163427

@@ -3424,13 +3435,10 @@ int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
34243435
* one does the construction and the others add to that.
34253436
*/
34263437
mutex_lock(&cxlrd->range_lock);
3427-
region_dev = device_find_child(&cxlrd->cxlsd.cxld.dev, hpa,
3428-
match_region_by_range);
3429-
if (!region_dev) {
3438+
struct cxl_region *cxlr __free(put_cxl_region) =
3439+
cxl_find_region_by_range(cxlrd, hpa);
3440+
if (!cxlr)
34303441
cxlr = construct_region(cxlrd, cxled);
3431-
region_dev = &cxlr->dev;
3432-
} else
3433-
cxlr = to_cxl_region(region_dev);
34343442
mutex_unlock(&cxlrd->range_lock);
34353443

34363444
rc = PTR_ERR_OR_ZERO(cxlr);
@@ -3455,8 +3463,6 @@ int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
34553463
p->res);
34563464
}
34573465

3458-
put_device(region_dev);
3459-
34603466
return rc;
34613467
}
34623468
EXPORT_SYMBOL_NS_GPL(cxl_add_to_region, "CXL");

drivers/cxl/cxl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,7 @@ struct cxl_root *find_cxl_root(struct cxl_port *port);
741741
DEFINE_FREE(put_cxl_root, struct cxl_root *, if (_T) put_device(&_T->port.dev))
742742
DEFINE_FREE(put_cxl_port, struct cxl_port *, if (!IS_ERR_OR_NULL(_T)) put_device(&_T->dev))
743743
DEFINE_FREE(put_cxl_root_decoder, struct cxl_root_decoder *, if (!IS_ERR_OR_NULL(_T)) put_device(&_T->cxlsd.cxld.dev))
744+
DEFINE_FREE(put_cxl_region, struct cxl_region *, if (!IS_ERR_OR_NULL(_T)) put_device(&_T->dev))
744745

745746
int devm_cxl_enumerate_ports(struct cxl_memdev *cxlmd);
746747
void cxl_bus_rescan(void);

0 commit comments

Comments
 (0)