@@ -1258,18 +1258,13 @@ EXPORT_SYMBOL_NS_GPL(devm_cxl_add_rch_dport, CXL);
12581258static int add_ep (struct cxl_ep * new )
12591259{
12601260 struct cxl_port * port = new -> dport -> port ;
1261- int rc ;
12621261
1263- device_lock (& port -> dev );
1264- if (port -> dead ) {
1265- device_unlock (& port -> dev );
1262+ guard (device )(& port -> dev );
1263+ if (port -> dead )
12661264 return - ENXIO ;
1267- }
1268- rc = xa_insert (& port -> endpoints , (unsigned long )new -> ep , new ,
1269- GFP_KERNEL );
1270- device_unlock (& port -> dev );
12711265
1272- return rc ;
1266+ return xa_insert (& port -> endpoints , (unsigned long )new -> ep ,
1267+ new , GFP_KERNEL );
12731268}
12741269
12751270/**
@@ -1393,14 +1388,14 @@ static void delete_endpoint(void *data)
13931388 struct cxl_port * endpoint = cxlmd -> endpoint ;
13941389 struct device * host = endpoint_host (endpoint );
13951390
1396- device_lock (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 );
1391+ scoped_guard (device , 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+ }
1397+ cxlmd -> endpoint = NULL ;
14011398 }
1402- cxlmd -> endpoint = NULL ;
1403- device_unlock (host );
14041399 put_device (& endpoint -> dev );
14051400 put_device (host );
14061401}
@@ -1565,40 +1560,38 @@ static int add_port_attach_ep(struct cxl_memdev *cxlmd,
15651560 * dereferencing the device of the port before the parent_port releasing.
15661561 */
15671562 struct cxl_port * port __free (put_cxl_port ) = NULL ;
1568- device_lock (& 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- port = ERR_PTR (- ENXIO );
1574- goto out ;
1575- }
1563+ scoped_guard (device , & 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+ return - ENXIO ;
1569+ }
1570+
1571+ port = find_cxl_port_at (parent_port , dport_dev , & dport );
1572+ if (!port ) {
1573+ component_reg_phys = find_component_registers (uport_dev );
1574+ port = devm_cxl_add_port (& parent_port -> dev , uport_dev ,
1575+ component_reg_phys , parent_dport );
1576+ if (IS_ERR (port ))
1577+ return PTR_ERR (port );
15761578
1577- port = find_cxl_port_at (parent_port , dport_dev , & dport );
1578- if (!port ) {
1579- component_reg_phys = find_component_registers (uport_dev );
1580- port = devm_cxl_add_port (& parent_port -> dev , uport_dev ,
1581- component_reg_phys , parent_dport );
1582- /* retry find to pick up the new dport information */
1583- if (!IS_ERR (port ))
1579+ /* retry find to pick up the new dport information */
15841580 port = find_cxl_port_at (parent_port , dport_dev , & dport );
1581+ if (!port )
1582+ return - ENXIO ;
1583+ }
15851584 }
1586- out :
1587- device_unlock (& parent_port -> dev );
15881585
1589- if (IS_ERR (port ))
1590- rc = PTR_ERR (port );
1591- else {
1592- dev_dbg (& cxlmd -> dev , "add to new port %s:%s\n" ,
1593- dev_name (& port -> dev ), dev_name (port -> uport_dev ));
1594- rc = cxl_add_ep (dport , & cxlmd -> dev );
1595- if (rc == - EBUSY ) {
1596- /*
1597- * "can't" happen, but this error code means
1598- * something to the caller, so translate it.
1599- */
1600- rc = - ENXIO ;
1601- }
1586+ dev_dbg (& cxlmd -> dev , "add to new port %s:%s\n" ,
1587+ dev_name (& port -> dev ), dev_name (port -> uport_dev ));
1588+ rc = cxl_add_ep (dport , & cxlmd -> dev );
1589+ if (rc == - EBUSY ) {
1590+ /*
1591+ * "can't" happen, but this error code means
1592+ * something to the caller, so translate it.
1593+ */
1594+ rc = - ENXIO ;
16021595 }
16031596
16041597 return rc ;
@@ -1979,7 +1972,6 @@ EXPORT_SYMBOL_NS_GPL(cxl_decoder_add_locked, CXL);
19791972int cxl_decoder_add (struct cxl_decoder * cxld , int * target_map )
19801973{
19811974 struct cxl_port * port ;
1982- int rc ;
19831975
19841976 if (WARN_ON_ONCE (!cxld ))
19851977 return - EINVAL ;
@@ -1989,11 +1981,8 @@ int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map)
19891981
19901982 port = to_cxl_port (cxld -> dev .parent );
19911983
1992- device_lock (& port -> dev );
1993- rc = cxl_decoder_add_locked (cxld , target_map );
1994- device_unlock (& port -> dev );
1995-
1996- return rc ;
1984+ guard (device )(& port -> dev );
1985+ return cxl_decoder_add_locked (cxld , target_map );
19971986}
19981987EXPORT_SYMBOL_NS_GPL (cxl_decoder_add , CXL );
19991988
0 commit comments