Re: [PATCH v2 2/3] cxl/port: Use scoped_guard()/guard() to drop device_lock() for cxl_port

Jonathan Cameron
2024-08-27T11:55:26Z

On Mon, 26 Aug 2024 08:30:57 +0000
Li Ming <ming4.li@intel.com> wrote:

> A device_lock() and device_unlock() pair can be replaced by a cleanup
> helper scoped_guard() or guard(), that can enhance code readability. In
> CXL subsystem, still use device_lock() and device_unlock() pairs for cxl
> port resource protection, most of them can be replaced by a
> scoped_guard() or a guard() simply.
>
> Suggested-by: Dan Williams <dan.j.williams@intel.com>
> Signed-off-by: Li Ming <ming4.li@intel.com>

Hi Li Ming,

The advantages of previous patch become clearer here. So
overall I think I'm convinced it's worth making the change.

Follow on comment below,

Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

> diff --git a/drivers/cxl/core/port.c b/drivers/cxl/core/port.c
> index b50dda6610e3..7b87b5142fa7 100644
> --- a/drivers/cxl/core/port.c
> +++ b/drivers/cxl/core/port.c

> @@ -1561,40 +1556,38 @@ static int add_port_attach_ep(struct cxl_memdev *cxlmd,
> return -EAGAIN;
> }
>
> - device_lock(&parent_port->dev);
> - if (!parent_port->dev.driver) {
> - dev_warn(&cxlmd->dev,
> - "port %s:%s disabled, failed to enumerate CXL.mem\n",
> - dev_name(&parent_port->dev), dev_name(uport_dev));
> - port = ERR_PTR(-ENXIO);
> - goto out;
> - }

As per comment in previous patch, I'd pull the instantiation of
port down here. That way constructor and destructor are at least nearby
and the ordering is more what I'd expect.

> + scoped_guard(device, &parent_port->dev) {
> + if (!parent_port->dev.driver) {
> + dev_warn(&cxlmd->dev,
> + "port %s:%s disabled, failed to enumerate CXL.mem\n",
> + dev_name(&parent_port->dev), dev_name(uport_dev));
> + return -ENXIO;
> + }
> +
> + port = find_cxl_port_at(parent_port, dport_dev, &dport);
> + if (!port) {
> + component_reg_phys = find_component_registers(uport_dev);
> + port = devm_cxl_add_port(&parent_port->dev, uport_dev,
> + component_reg_phys, parent_dport);
> + if (IS_ERR(port))
> + return PTR_ERR(port);
>
> - port = find_cxl_port_at(parent_port, dport_dev, &dport);
> - if (!port) {
> - component_reg_phys = find_component_registers(uport_dev);
> - port = devm_cxl_add_port(&parent_port->dev, uport_dev,
> - component_reg_phys, parent_dport);
> - /* retry find to pick up the new dport information */
> - if (!IS_ERR(port))
> + /* retry find to pick up the new dport information */
> port = find_cxl_port_at(parent_port, dport_dev, &dport);
> + if (!port)
> + return -ENXIO;
> + }
> }
> -out:
> - device_unlock(&parent_port->dev);
>
> - if (IS_ERR(port))
> - rc = PTR_ERR(port);
> - else {
> - dev_dbg(&cxlmd->dev, "add to new port %s:%s\n",
> - dev_name(&port->dev), dev_name(port->uport_dev));
> - rc = cxl_add_ep(dport, &cxlmd->dev);
> - if (rc == -EBUSY) {
> - /*
> - * "can't" happen, but this error code means
> - * something to the caller, so translate it.
> - */
> - rc = -ENXIO;
> - }
> + dev_dbg(&cxlmd->dev, "add to new port %s:%s\n",
> + dev_name(&port->dev), dev_name(port->uport_dev));
> + rc = cxl_add_ep(dport, &cxlmd->dev);
> + if (rc == -EBUSY) {
> + /*
> + * "can't" happen, but this error code means
> + * something to the caller, so translate it.
> + */
> + rc = -ENXIO;
> }
>
> return rc;