@@ -1477,12 +1477,11 @@ static void cxl_detach_ep(void *data)
14771477 .cxlmd = cxlmd ,
14781478 .depth = i ,
14791479 };
1480- struct device * dev ;
14811480 struct cxl_ep * ep ;
14821481 bool died = false;
14831482
1484- dev = bus_find_device ( & cxl_bus_type , NULL , & ctx ,
1485- port_has_memdev );
1483+ struct device * dev __free ( put_device ) =
1484+ bus_find_device ( & cxl_bus_type , NULL , & ctx , port_has_memdev );
14861485 if (!dev )
14871486 continue ;
14881487 port = to_cxl_port (dev );
@@ -1512,7 +1511,6 @@ static void cxl_detach_ep(void *data)
15121511 dev_name (& port -> dev ));
15131512 delete_switch_port (port );
15141513 }
1515- put_device (& port -> dev );
15161514 device_unlock (& parent_port -> dev );
15171515 }
15181516}
@@ -1540,7 +1538,6 @@ static int add_port_attach_ep(struct cxl_memdev *cxlmd,
15401538 struct device * dport_dev )
15411539{
15421540 struct device * dparent = grandparent (dport_dev );
1543- struct cxl_port * port , * parent_port = NULL ;
15441541 struct cxl_dport * dport , * parent_dport ;
15451542 resource_size_t component_reg_phys ;
15461543 int rc ;
@@ -1556,12 +1553,18 @@ static int add_port_attach_ep(struct cxl_memdev *cxlmd,
15561553 return - ENXIO ;
15571554 }
15581555
1559- parent_port = find_cxl_port (dparent , & parent_dport );
1556+ struct cxl_port * parent_port __free (put_cxl_port ) =
1557+ find_cxl_port (dparent , & parent_dport );
15601558 if (!parent_port ) {
15611559 /* iterate to create this parent_port */
15621560 return - EAGAIN ;
15631561 }
15641562
1563+ /*
1564+ * Definition with __free() here to keep the sequence of
1565+ * dereferencing the device of the port before the parent_port releasing.
1566+ */
1567+ struct cxl_port * port __free (put_cxl_port ) = NULL ;
15651568 device_lock (& parent_port -> dev );
15661569 if (!parent_port -> dev .driver ) {
15671570 dev_warn (& cxlmd -> dev ,
@@ -1596,10 +1599,8 @@ static int add_port_attach_ep(struct cxl_memdev *cxlmd,
15961599 */
15971600 rc = - ENXIO ;
15981601 }
1599- put_device (& port -> dev );
16001602 }
16011603
1602- put_device (& parent_port -> dev );
16031604 return rc ;
16041605}
16051606
@@ -1630,7 +1631,6 @@ int devm_cxl_enumerate_ports(struct cxl_memdev *cxlmd)
16301631 struct device * dport_dev = grandparent (iter );
16311632 struct device * uport_dev ;
16321633 struct cxl_dport * dport ;
1633- struct cxl_port * port ;
16341634
16351635 /*
16361636 * The terminal "grandparent" in PCI is NULL and @platform_bus
@@ -1649,7 +1649,8 @@ int devm_cxl_enumerate_ports(struct cxl_memdev *cxlmd)
16491649 dev_dbg (dev , "scan: iter: %s dport_dev: %s parent: %s\n" ,
16501650 dev_name (iter ), dev_name (dport_dev ),
16511651 dev_name (uport_dev ));
1652- port = find_cxl_port (dport_dev , & dport );
1652+ struct cxl_port * port __free (put_cxl_port ) =
1653+ find_cxl_port (dport_dev , & dport );
16531654 if (port ) {
16541655 dev_dbg (& cxlmd -> dev ,
16551656 "found already registered port %s:%s\n" ,
@@ -1664,18 +1665,13 @@ int devm_cxl_enumerate_ports(struct cxl_memdev *cxlmd)
16641665 * the parent_port lock as the current port may be being
16651666 * reaped.
16661667 */
1667- if (rc && rc != - EBUSY ) {
1668- put_device (& port -> dev );
1668+ if (rc && rc != - EBUSY )
16691669 return rc ;
1670- }
16711670
16721671 /* Any more ports to add between this one and the root? */
1673- if (!dev_is_cxl_root_child (& port -> dev )) {
1674- put_device (& port -> dev );
1672+ if (!dev_is_cxl_root_child (& port -> dev ))
16751673 continue ;
1676- }
16771674
1678- put_device (& port -> dev );
16791675 return 0 ;
16801676 }
16811677
0 commit comments