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: Use scoped_guard()/guard() to drop device_lock() for cxl_port

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>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Link: https://patch.msgid.link/20240830013138.2256244-2-ming4.li@intel.com
Signed-off-by: Dave Jiang <dave.jiang@intel.com>

authored by

Li Ming and committed by
Dave Jiang
7f569e91 dd2617eb

+75 -90
+1 -2
drivers/cxl/core/mbox.c
··· 1214 1214 int rc; 1215 1215 1216 1216 /* synchronize with cxl_mem_probe() and decoder write operations */ 1217 - device_lock(&cxlmd->dev); 1217 + guard(device)(&cxlmd->dev); 1218 1218 endpoint = cxlmd->endpoint; 1219 1219 down_read(&cxl_region_rwsem); 1220 1220 /* ··· 1226 1226 else 1227 1227 rc = -EBUSY; 1228 1228 up_read(&cxl_region_rwsem); 1229 - device_unlock(&cxlmd->dev); 1230 1229 1231 1230 return rc; 1232 1231 }
+44 -55
drivers/cxl/core/port.c
··· 1258 1258 static int add_ep(struct cxl_ep *new) 1259 1259 { 1260 1260 struct cxl_port *port = new->dport->port; 1261 - int rc; 1262 1261 1263 - device_lock(&port->dev); 1264 - if (port->dead) { 1265 - device_unlock(&port->dev); 1262 + guard(device)(&port->dev); 1263 + if (port->dead) 1266 1264 return -ENXIO; 1267 - } 1268 - rc = xa_insert(&port->endpoints, (unsigned long)new->ep, new, 1269 - GFP_KERNEL); 1270 - device_unlock(&port->dev); 1271 1265 1272 - return rc; 1266 + return xa_insert(&port->endpoints, (unsigned long)new->ep, 1267 + new, GFP_KERNEL); 1273 1268 } 1274 1269 1275 1270 /** ··· 1388 1393 struct cxl_port *endpoint = cxlmd->endpoint; 1389 1394 struct device *host = endpoint_host(endpoint); 1390 1395 1391 - device_lock(host); 1392 - if (host->driver && !endpoint->dead) { 1393 - devm_release_action(host, cxl_unlink_parent_dport, endpoint); 1394 - devm_release_action(host, cxl_unlink_uport, endpoint); 1395 - devm_release_action(host, unregister_port, endpoint); 1396 + scoped_guard(device, host) { 1397 + if (host->driver && !endpoint->dead) { 1398 + devm_release_action(host, cxl_unlink_parent_dport, endpoint); 1399 + devm_release_action(host, cxl_unlink_uport, endpoint); 1400 + devm_release_action(host, unregister_port, endpoint); 1401 + } 1402 + cxlmd->endpoint = NULL; 1396 1403 } 1397 - cxlmd->endpoint = NULL; 1398 - device_unlock(host); 1399 1404 put_device(&endpoint->dev); 1400 1405 put_device(host); 1401 1406 } ··· 1560 1565 * dereferencing the device of the port before the parent_port releasing. 1561 1566 */ 1562 1567 struct cxl_port *port __free(put_cxl_port) = NULL; 1563 - device_lock(&parent_port->dev); 1564 - if (!parent_port->dev.driver) { 1565 - dev_warn(&cxlmd->dev, 1566 - "port %s:%s disabled, failed to enumerate CXL.mem\n", 1567 - dev_name(&parent_port->dev), dev_name(uport_dev)); 1568 - port = ERR_PTR(-ENXIO); 1569 - goto out; 1570 - } 1571 - 1572 - port = find_cxl_port_at(parent_port, dport_dev, &dport); 1573 - if (!port) { 1574 - component_reg_phys = find_component_registers(uport_dev); 1575 - port = devm_cxl_add_port(&parent_port->dev, uport_dev, 1576 - component_reg_phys, parent_dport); 1577 - /* retry find to pick up the new dport information */ 1578 - if (!IS_ERR(port)) 1579 - port = find_cxl_port_at(parent_port, dport_dev, &dport); 1580 - } 1581 - out: 1582 - device_unlock(&parent_port->dev); 1583 - 1584 - if (IS_ERR(port)) 1585 - rc = PTR_ERR(port); 1586 - else { 1587 - dev_dbg(&cxlmd->dev, "add to new port %s:%s\n", 1588 - dev_name(&port->dev), dev_name(port->uport_dev)); 1589 - rc = cxl_add_ep(dport, &cxlmd->dev); 1590 - if (rc == -EBUSY) { 1591 - /* 1592 - * "can't" happen, but this error code means 1593 - * something to the caller, so translate it. 1594 - */ 1595 - rc = -ENXIO; 1568 + scoped_guard(device, &parent_port->dev) { 1569 + if (!parent_port->dev.driver) { 1570 + dev_warn(&cxlmd->dev, 1571 + "port %s:%s disabled, failed to enumerate CXL.mem\n", 1572 + dev_name(&parent_port->dev), dev_name(uport_dev)); 1573 + return -ENXIO; 1596 1574 } 1575 + 1576 + port = find_cxl_port_at(parent_port, dport_dev, &dport); 1577 + if (!port) { 1578 + component_reg_phys = find_component_registers(uport_dev); 1579 + port = devm_cxl_add_port(&parent_port->dev, uport_dev, 1580 + component_reg_phys, parent_dport); 1581 + if (IS_ERR(port)) 1582 + return PTR_ERR(port); 1583 + 1584 + /* retry find to pick up the new dport information */ 1585 + port = find_cxl_port_at(parent_port, dport_dev, &dport); 1586 + if (!port) 1587 + return -ENXIO; 1588 + } 1589 + } 1590 + 1591 + dev_dbg(&cxlmd->dev, "add to new port %s:%s\n", 1592 + dev_name(&port->dev), dev_name(port->uport_dev)); 1593 + rc = cxl_add_ep(dport, &cxlmd->dev); 1594 + if (rc == -EBUSY) { 1595 + /* 1596 + * "can't" happen, but this error code means 1597 + * something to the caller, so translate it. 1598 + */ 1599 + rc = -ENXIO; 1597 1600 } 1598 1601 1599 1602 return rc; ··· 1972 1979 int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map) 1973 1980 { 1974 1981 struct cxl_port *port; 1975 - int rc; 1976 1982 1977 1983 if (WARN_ON_ONCE(!cxld)) 1978 1984 return -EINVAL; ··· 1981 1989 1982 1990 port = to_cxl_port(cxld->dev.parent); 1983 1991 1984 - device_lock(&port->dev); 1985 - rc = cxl_decoder_add_locked(cxld, target_map); 1986 - device_unlock(&port->dev); 1987 - 1988 - return rc; 1992 + guard(device)(&port->dev); 1993 + return cxl_decoder_add_locked(cxld, target_map); 1989 1994 } 1990 1995 EXPORT_SYMBOL_NS_GPL(cxl_decoder_add, CXL); 1991 1996
+13 -12
drivers/cxl/core/region.c
··· 3094 3094 struct cxl_region *cxlr = _cxlr; 3095 3095 struct cxl_nvdimm_bridge *cxl_nvb = cxlr->cxl_nvb; 3096 3096 3097 - device_lock(&cxl_nvb->dev); 3098 - if (cxlr->cxlr_pmem) 3099 - devm_release_action(&cxl_nvb->dev, cxlr_pmem_unregister, 3100 - cxlr->cxlr_pmem); 3101 - device_unlock(&cxl_nvb->dev); 3097 + scoped_guard(device, &cxl_nvb->dev) { 3098 + if (cxlr->cxlr_pmem) 3099 + devm_release_action(&cxl_nvb->dev, cxlr_pmem_unregister, 3100 + cxlr->cxlr_pmem); 3101 + } 3102 3102 cxlr->cxl_nvb = NULL; 3103 3103 put_device(&cxl_nvb->dev); 3104 3104 } ··· 3134 3134 dev_dbg(&cxlr->dev, "%s: register %s\n", dev_name(dev->parent), 3135 3135 dev_name(dev)); 3136 3136 3137 - device_lock(&cxl_nvb->dev); 3138 - if (cxl_nvb->dev.driver) 3139 - rc = devm_add_action_or_reset(&cxl_nvb->dev, 3140 - cxlr_pmem_unregister, cxlr_pmem); 3141 - else 3142 - rc = -ENXIO; 3143 - device_unlock(&cxl_nvb->dev); 3137 + scoped_guard(device, &cxl_nvb->dev) { 3138 + if (cxl_nvb->dev.driver) 3139 + rc = devm_add_action_or_reset(&cxl_nvb->dev, 3140 + cxlr_pmem_unregister, 3141 + cxlr_pmem); 3142 + else 3143 + rc = -ENXIO; 3144 + } 3144 3145 3145 3146 if (rc) 3146 3147 goto err_bridge;
+10 -12
drivers/cxl/mem.c
··· 168 168 169 169 cxl_setup_parent_dport(dev, dport); 170 170 171 - device_lock(endpoint_parent); 172 - if (!endpoint_parent->driver) { 173 - dev_err(dev, "CXL port topology %s not enabled\n", 174 - dev_name(endpoint_parent)); 175 - rc = -ENXIO; 176 - goto unlock; 177 - } 171 + scoped_guard(device, endpoint_parent) { 172 + if (!endpoint_parent->driver) { 173 + dev_err(dev, "CXL port topology %s not enabled\n", 174 + dev_name(endpoint_parent)); 175 + return -ENXIO; 176 + } 178 177 179 - rc = devm_cxl_add_endpoint(endpoint_parent, cxlmd, dport); 180 - unlock: 181 - device_unlock(endpoint_parent); 182 - if (rc) 183 - return rc; 178 + rc = devm_cxl_add_endpoint(endpoint_parent, cxlmd, dport); 179 + if (rc) 180 + return rc; 181 + } 184 182 185 183 /* 186 184 * The kernel may be operating out of CXL memory on this device,
+7 -9
drivers/cxl/pmem.c
··· 233 233 if (!is_cxl_nvdimm(dev)) 234 234 return 0; 235 235 236 - device_lock(dev); 237 - if (!dev->driver) 238 - goto out; 239 - 240 - cxl_nvd = to_cxl_nvdimm(dev); 241 - if (cxl_nvd->cxlmd && cxl_nvd->cxlmd->cxl_nvb == data) 242 - release = true; 243 - out: 244 - device_unlock(dev); 236 + scoped_guard(device, dev) { 237 + if (dev->driver) { 238 + cxl_nvd = to_cxl_nvdimm(dev); 239 + if (cxl_nvd->cxlmd && cxl_nvd->cxlmd->cxl_nvb == data) 240 + release = true; 241 + } 242 + } 245 243 if (release) 246 244 device_release_driver(dev); 247 245 return 0;