In function cxl_add_to_region() there is code to determine the root
decoder associated to an endpoint decoder. Factor out that code for
later reuse. This has the benefit of reducing cxl_add_to_region()'s
function complexity.
The reference of cxlrd_dev can be freed earlier. Since the root
decoder exists as long as the root port exists and the endpoint
already holds a reference to the root port, this additional reference
is not needed. Though it looks obvious to use __free() for the
reference of cxlrd_dev here too, this is done in a later rework. So
just move the code.
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>
Tested-by: Gregory Price <gourry@gourry.net>
---
drivers/cxl/core/region.c | 55 ++++++++++++++++++++++++++-------------
1 file changed, 37 insertions(+), 18 deletions(-)
diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
index 8244a27d0fd6..7d9d9b8f9eea 100644
--- a/drivers/cxl/core/region.c
+++ b/drivers/cxl/core/region.c
@@ -3212,6 +3212,38 @@ static int match_root_decoder_by_range(struct device *dev,
return range_contains(r1, r2);
}
+static struct cxl_root_decoder *
+cxl_find_root_decoder(struct cxl_endpoint_decoder *cxled)
+{
+ struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
+ struct cxl_port *port = cxled_to_port(cxled);
+ struct cxl_root *cxl_root __free(put_cxl_root) = find_cxl_root(port);
+ struct cxl_decoder *cxld = &cxled->cxld;
+ struct range *hpa = &cxld->hpa_range;
+ struct device *cxlrd_dev;
+
+ cxlrd_dev = device_find_child(&cxl_root->port.dev, hpa,
+ match_root_decoder_by_range);
+ if (!cxlrd_dev) {
+ dev_err(cxlmd->dev.parent,
+ "%s:%s no CXL window for range %#llx:%#llx\n",
+ dev_name(&cxlmd->dev), dev_name(&cxld->dev),
+ cxld->hpa_range.start, cxld->hpa_range.end);
+ return NULL;
+ }
+
+ /*
+ * device_find_child() created a reference to the root
+ * decoder. Since the root decoder exists as long as the root
+ * port exists and the endpoint already holds a reference to
+ * the root port, this additional reference is not needed.
+ * Free it here.
+ */
+ put_device(cxlrd_dev);
+
+ return to_cxl_root_decoder(cxlrd_dev);
+}
+
static int match_region_by_range(struct device *dev, const void *data)
{
struct cxl_region_params *p;
@@ -3386,29 +3418,17 @@ static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
{
- struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
- struct cxl_port *port = cxled_to_port(cxled);
- struct cxl_root *cxl_root __free(put_cxl_root) = find_cxl_root(port);
struct range *hpa = &cxled->cxld.hpa_range;
- struct cxl_decoder *cxld = &cxled->cxld;
- struct device *cxlrd_dev, *region_dev;
+ struct device *region_dev;
struct cxl_root_decoder *cxlrd;
struct cxl_region_params *p;
struct cxl_region *cxlr;
bool attach = false;
int rc;
- cxlrd_dev = device_find_child(&cxl_root->port.dev, &cxld->hpa_range,
- match_root_decoder_by_range);
- if (!cxlrd_dev) {
- dev_err(cxlmd->dev.parent,
- "%s:%s no CXL window for range %#llx:%#llx\n",
- dev_name(&cxlmd->dev), dev_name(&cxld->dev),
- cxld->hpa_range.start, cxld->hpa_range.end);
+ cxlrd = cxl_find_root_decoder(cxled);
+ if (!cxlrd)
return -ENXIO;
- }
-
- cxlrd = to_cxl_root_decoder(cxlrd_dev);
/*
* Ensure that if multiple threads race to construct_region() for @hpa
@@ -3426,7 +3446,7 @@ int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
rc = PTR_ERR_OR_ZERO(cxlr);
if (rc)
- goto out;
+ return rc;
attach_target(cxlr, cxled, -1, TASK_UNINTERRUPTIBLE);
@@ -3447,8 +3467,7 @@ int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
}
put_device(region_dev);
-out:
- put_device(cxlrd_dev);
+
return rc;
}
EXPORT_SYMBOL_NS_GPL(cxl_add_to_region, "CXL");
--
2.39.5
On Thu, Mar 06, 2025 at 05:44:42PM +0100, Robert Richter wrote: > In function cxl_add_to_region() there is code to determine the root > decoder associated to an endpoint decoder. Factor out that code for > later reuse. This has the benefit of reducing cxl_add_to_region()'s > function complexity. > > The reference of cxlrd_dev can be freed earlier. Since the root > decoder exists as long as the root port exists and the endpoint > already holds a reference to the root port, this additional reference > is not needed. Though it looks obvious to use __free() for the > reference of cxlrd_dev here too, this is done in a later rework. So > just move the code. > > 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> > Tested-by: Gregory Price <gourry@gourry.net> Reviewed-by: Alison Schofield <alison.schofield@intel.com>
Robert Richter wrote: > In function cxl_add_to_region() there is code to determine the root > decoder associated to an endpoint decoder. Factor out that code for > later reuse. This has the benefit of reducing cxl_add_to_region()'s > function complexity. > > The reference of cxlrd_dev can be freed earlier. Since the root > decoder exists as long as the root port exists and the endpoint > already holds a reference to the root port, this additional reference > is not needed. Though it looks obvious to use __free() for the > reference of cxlrd_dev here too, this is done in a later rework. So > just move the code. > > 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> > Tested-by: Gregory Price <gourry@gourry.net> Looks good to me: Reviewed-by: Dan Williams <dan.j.williams@intel.com>
On Thursday, March 6, 2025 5:44:42 PM Central European Summer Time Robert Richter wrote:
> In function cxl_add_to_region() there is code to determine the root
> decoder associated to an endpoint decoder. Factor out that code for
> later reuse. This has the benefit of reducing cxl_add_to_region()'s
> function complexity.
>
> The reference of cxlrd_dev can be freed earlier. Since the root
> decoder exists as long as the root port exists and the endpoint
> already holds a reference to the root port, this additional reference
> is not needed. Though it looks obvious to use __free() for the
> reference of cxlrd_dev here too, this is done in a later rework. So
> just move the code.
>
> 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>
> Tested-by: Gregory Price <gourry@gourry.net>
> ---
Reviewed-by: Fabio M. De Francesco <fabio.m.de.francesco@linux.intel.com>
> drivers/cxl/core/region.c | 55 ++++++++++++++++++++++++++-------------
> 1 file changed, 37 insertions(+), 18 deletions(-)
>
> diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
> index 8244a27d0fd6..7d9d9b8f9eea 100644
> --- a/drivers/cxl/core/region.c
> +++ b/drivers/cxl/core/region.c
> @@ -3212,6 +3212,38 @@ static int match_root_decoder_by_range(struct device *dev,
> return range_contains(r1, r2);
> }
>
> +static struct cxl_root_decoder *
> +cxl_find_root_decoder(struct cxl_endpoint_decoder *cxled)
> +{
> + struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
> + struct cxl_port *port = cxled_to_port(cxled);
> + struct cxl_root *cxl_root __free(put_cxl_root) = find_cxl_root(port);
> + struct cxl_decoder *cxld = &cxled->cxld;
> + struct range *hpa = &cxld->hpa_range;
> + struct device *cxlrd_dev;
> +
> + cxlrd_dev = device_find_child(&cxl_root->port.dev, hpa,
> + match_root_decoder_by_range);
> + if (!cxlrd_dev) {
> + dev_err(cxlmd->dev.parent,
> + "%s:%s no CXL window for range %#llx:%#llx\n",
> + dev_name(&cxlmd->dev), dev_name(&cxld->dev),
> + cxld->hpa_range.start, cxld->hpa_range.end);
> + return NULL;
> + }
> +
> + /*
> + * device_find_child() created a reference to the root
> + * decoder. Since the root decoder exists as long as the root
> + * port exists and the endpoint already holds a reference to
> + * the root port, this additional reference is not needed.
> + * Free it here.
> + */
> + put_device(cxlrd_dev);
> +
> + return to_cxl_root_decoder(cxlrd_dev);
> +}
> +
> static int match_region_by_range(struct device *dev, const void *data)
> {
> struct cxl_region_params *p;
> @@ -3386,29 +3418,17 @@ static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
>
> int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
> {
> - struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
> - struct cxl_port *port = cxled_to_port(cxled);
> - struct cxl_root *cxl_root __free(put_cxl_root) = find_cxl_root(port);
> struct range *hpa = &cxled->cxld.hpa_range;
> - struct cxl_decoder *cxld = &cxled->cxld;
> - struct device *cxlrd_dev, *region_dev;
> + struct device *region_dev;
> struct cxl_root_decoder *cxlrd;
> struct cxl_region_params *p;
> struct cxl_region *cxlr;
> bool attach = false;
> int rc;
>
> - cxlrd_dev = device_find_child(&cxl_root->port.dev, &cxld->hpa_range,
> - match_root_decoder_by_range);
> - if (!cxlrd_dev) {
> - dev_err(cxlmd->dev.parent,
> - "%s:%s no CXL window for range %#llx:%#llx\n",
> - dev_name(&cxlmd->dev), dev_name(&cxld->dev),
> - cxld->hpa_range.start, cxld->hpa_range.end);
> + cxlrd = cxl_find_root_decoder(cxled);
> + if (!cxlrd)
> return -ENXIO;
> - }
> -
> - cxlrd = to_cxl_root_decoder(cxlrd_dev);
>
> /*
> * Ensure that if multiple threads race to construct_region() for @hpa
> @@ -3426,7 +3446,7 @@ int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
>
> rc = PTR_ERR_OR_ZERO(cxlr);
> if (rc)
> - goto out;
> + return rc;
>
> attach_target(cxlr, cxled, -1, TASK_UNINTERRUPTIBLE);
>
> @@ -3447,8 +3467,7 @@ int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
> }
>
> put_device(region_dev);
> -out:
> - put_device(cxlrd_dev);
> +
> return rc;
> }
> EXPORT_SYMBOL_NS_GPL(cxl_add_to_region, "CXL");
>
© 2016 - 2026 Red Hat, Inc.