Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <dan.j.williams@intel.com>
Signed-off-by: Li Ming <ming4.li@intel.com>
Reviewed-by: Ira Weiny <ira.weiny@intel.com>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huwaei.com>
Link: https://patch.msgid.link/20240830013138.2256244-3-ming4.li@intel.com
Signed-off-by: Dave Jiang <dave.jiang@intel.com>

authored by

Li Ming and committed by
Dave Jiang
91c0e9d6 7f569e91

+35 -24
+35 -24
drivers/cxl/core/port.c
··· 828 828 &cxl_einj_inject_fops); 829 829 } 830 830 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) 835 834 { 836 - struct cxl_port *port; 837 - struct device *dev; 835 + struct device *dev __free(put_device) = &port->dev; 838 836 int rc; 839 837 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); 847 840 struct cxl_dev_state *cxlds = cxlmd->cxlds; 848 841 849 842 rc = dev_set_name(dev, "endpoint%d", port->id); 850 843 if (rc) 851 - goto err; 844 + return rc; 852 845 853 846 /* 854 847 * The endpoint driver already enumerated the component and RAS ··· 854 861 } else if (parent_dport) { 855 862 rc = dev_set_name(dev, "port%d", port->id); 856 863 if (rc) 857 - goto err; 864 + return rc; 858 865 859 866 rc = cxl_port_setup_regs(port, component_reg_phys); 860 867 if (rc) 861 - goto err; 862 - } else 868 + return rc; 869 + } else { 863 870 rc = dev_set_name(dev, "root%d", port->id); 864 - if (rc) 865 - goto err; 871 + if (rc) 872 + return rc; 873 + } 866 874 867 875 rc = device_add(dev); 868 876 if (rc) 869 - goto err; 877 + return rc; 878 + 879 + /* Inhibit the cleanup function invoked */ 880 + dev = NULL; 881 + return 0; 882 + } 883 + 884 + static struct cxl_port *__devm_cxl_add_port(struct device *host, 885 + struct device *uport_dev, 886 + resource_size_t component_reg_phys, 887 + struct cxl_dport *parent_dport) 888 + { 889 + struct cxl_port *port; 890 + int rc; 891 + 892 + port = cxl_port_alloc(uport_dev, parent_dport); 893 + if (IS_ERR(port)) 894 + return port; 895 + 896 + rc = cxl_port_add(port, component_reg_phys, parent_dport); 897 + if (rc) 898 + return ERR_PTR(rc); 870 899 871 900 rc = devm_add_action_or_reset(host, unregister_port, port); 872 901 if (rc) ··· 906 891 port->pci_latency = cxl_pci_get_latency(to_pci_dev(uport_dev)); 907 892 908 893 return port; 909 - 910 - err: 911 - put_device(dev); 912 - return ERR_PTR(rc); 913 894 } 914 895 915 896 /**