Skip to content

Commit 91c0e9d

Browse files
ming4lidavejiang
authored andcommitted
cxl/port: Refactor __devm_cxl_add_port() to drop goto pattern
In __devm_cxl_add_port(), there is a 'goto' to call put_device() for the error cases between device_initialize() and device_add() to dereference the 'struct device' of a new cxl_port. The 'goto' pattern in the case can be removed by refactoring. Introducing a new function called cxl_port_add() which is used to add the 'struct device' of a new cxl_port to device hierarchy, moving the functions needing the help of the 'goto' into cxl_port_add(), and using a scoped-based resource management __free() to drop the open coded put_device() and the 'goto' for the error cases. Suggested-by: Dan Williams <[email protected]> Signed-off-by: Li Ming <[email protected]> Reviewed-by: Ira Weiny <[email protected]> Reviewed-by: Jonathan Cameron <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Dave Jiang <[email protected]>
1 parent 7f569e9 commit 91c0e9d

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

drivers/cxl/core/port.c

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -828,27 +828,20 @@ static void cxl_debugfs_create_dport_dir(struct cxl_dport *dport)
828828
&cxl_einj_inject_fops);
829829
}
830830

831-
static struct cxl_port *__devm_cxl_add_port(struct device *host,
832-
struct device *uport_dev,
833-
resource_size_t component_reg_phys,
834-
struct cxl_dport *parent_dport)
831+
static int cxl_port_add(struct cxl_port *port,
832+
resource_size_t component_reg_phys,
833+
struct cxl_dport *parent_dport)
835834
{
836-
struct cxl_port *port;
837-
struct device *dev;
835+
struct device *dev __free(put_device) = &port->dev;
838836
int rc;
839837

840-
port = cxl_port_alloc(uport_dev, parent_dport);
841-
if (IS_ERR(port))
842-
return port;
843-
844-
dev = &port->dev;
845-
if (is_cxl_memdev(uport_dev)) {
846-
struct cxl_memdev *cxlmd = to_cxl_memdev(uport_dev);
838+
if (is_cxl_memdev(port->uport_dev)) {
839+
struct cxl_memdev *cxlmd = to_cxl_memdev(port->uport_dev);
847840
struct cxl_dev_state *cxlds = cxlmd->cxlds;
848841

849842
rc = dev_set_name(dev, "endpoint%d", port->id);
850843
if (rc)
851-
goto err;
844+
return rc;
852845

853846
/*
854847
* The endpoint driver already enumerated the component and RAS
@@ -861,19 +854,41 @@ static struct cxl_port *__devm_cxl_add_port(struct device *host,
861854
} else if (parent_dport) {
862855
rc = dev_set_name(dev, "port%d", port->id);
863856
if (rc)
864-
goto err;
857+
return rc;
865858

866859
rc = cxl_port_setup_regs(port, component_reg_phys);
867860
if (rc)
868-
goto err;
869-
} else
861+
return rc;
862+
} else {
870863
rc = dev_set_name(dev, "root%d", port->id);
871-
if (rc)
872-
goto err;
864+
if (rc)
865+
return rc;
866+
}
873867

874868
rc = device_add(dev);
875869
if (rc)
876-
goto err;
870+
return rc;
871+
872+
/* Inhibit the cleanup function invoked */
873+
dev = NULL;
874+
return 0;
875+
}
876+
877+
static struct cxl_port *__devm_cxl_add_port(struct device *host,
878+
struct device *uport_dev,
879+
resource_size_t component_reg_phys,
880+
struct cxl_dport *parent_dport)
881+
{
882+
struct cxl_port *port;
883+
int rc;
884+
885+
port = cxl_port_alloc(uport_dev, parent_dport);
886+
if (IS_ERR(port))
887+
return port;
888+
889+
rc = cxl_port_add(port, component_reg_phys, parent_dport);
890+
if (rc)
891+
return ERR_PTR(rc);
877892

878893
rc = devm_add_action_or_reset(host, unregister_port, port);
879894
if (rc)
@@ -891,10 +906,6 @@ static struct cxl_port *__devm_cxl_add_port(struct device *host,
891906
port->pci_latency = cxl_pci_get_latency(to_pci_dev(uport_dev));
892907

893908
return port;
894-
895-
err:
896-
put_device(dev);
897-
return ERR_PTR(rc);
898909
}
899910

900911
/**

0 commit comments

Comments
 (0)