Skip to content

Commit dd2617e

Browse files
ming4lidavejiang
authored andcommitted
cxl/port: Use __free() to drop put_device() for cxl_port
Using scope-based resource management __free() marco with a new helper called put_cxl_port() to drop open coded the put_device() used to dereference the 'struct device' in cxl_port. Suggested-by: Dan Williams <[email protected]> Signed-off-by: Li Ming <[email protected]> Reviewed-by: Jonathan Cameron <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Dave Jiang <[email protected]>
1 parent fa724cd commit dd2617e

File tree

5 files changed

+20
-29
lines changed

5 files changed

+20
-29
lines changed

drivers/cxl/core/pci.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -915,15 +915,13 @@ static void cxl_handle_rdport_errors(struct cxl_dev_state *cxlds)
915915
struct pci_dev *pdev = to_pci_dev(cxlds->dev);
916916
struct aer_capability_regs aer_regs;
917917
struct cxl_dport *dport;
918-
struct cxl_port *port;
919918
int severity;
920919

921-
port = cxl_pci_find_port(pdev, &dport);
920+
struct cxl_port *port __free(put_cxl_port) =
921+
cxl_pci_find_port(pdev, &dport);
922922
if (!port)
923923
return;
924924

925-
put_device(&port->dev);
926-
927925
if (!cxl_rch_get_aer_info(dport->regs.dport_aer, &aer_regs))
928926
return;
929927

drivers/cxl/core/port.c

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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

drivers/cxl/cxl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,7 @@ struct cxl_root *find_cxl_root(struct cxl_port *port);
744744
void put_cxl_root(struct cxl_root *cxl_root);
745745
DEFINE_FREE(put_cxl_root, struct cxl_root *, if (_T) put_cxl_root(_T))
746746

747+
DEFINE_FREE(put_cxl_port, struct cxl_port *, if (!IS_ERR_OR_NULL(_T)) put_device(&_T->dev))
747748
int devm_cxl_enumerate_ports(struct cxl_memdev *cxlmd);
748749
void cxl_bus_rescan(void);
749750
void cxl_bus_drain(void);

drivers/cxl/mem.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ static int cxl_mem_probe(struct device *dev)
109109
struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlmd->cxlds);
110110
struct cxl_dev_state *cxlds = cxlmd->cxlds;
111111
struct device *endpoint_parent;
112-
struct cxl_port *parent_port;
113112
struct cxl_dport *dport;
114113
struct dentry *dentry;
115114
int rc;
@@ -146,7 +145,8 @@ static int cxl_mem_probe(struct device *dev)
146145
if (rc)
147146
return rc;
148147

149-
parent_port = cxl_mem_find_port(cxlmd, &dport);
148+
struct cxl_port *parent_port __free(put_cxl_port) =
149+
cxl_mem_find_port(cxlmd, &dport);
150150
if (!parent_port) {
151151
dev_err(dev, "CXL port topology not found\n");
152152
return -ENXIO;
@@ -179,7 +179,6 @@ static int cxl_mem_probe(struct device *dev)
179179
rc = devm_cxl_add_endpoint(endpoint_parent, cxlmd, dport);
180180
unlock:
181181
device_unlock(endpoint_parent);
182-
put_device(&parent_port->dev);
183182
if (rc)
184183
return rc;
185184

drivers/cxl/pci.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,6 @@ static bool is_cxl_restricted(struct pci_dev *pdev)
473473
static int cxl_rcrb_get_comp_regs(struct pci_dev *pdev,
474474
struct cxl_register_map *map)
475475
{
476-
struct cxl_port *port;
477476
struct cxl_dport *dport;
478477
resource_size_t component_reg_phys;
479478

@@ -482,14 +481,12 @@ static int cxl_rcrb_get_comp_regs(struct pci_dev *pdev,
482481
.resource = CXL_RESOURCE_NONE,
483482
};
484483

485-
port = cxl_pci_find_port(pdev, &dport);
484+
struct cxl_port *port __free(put_cxl_port) =
485+
cxl_pci_find_port(pdev, &dport);
486486
if (!port)
487487
return -EPROBE_DEFER;
488488

489489
component_reg_phys = cxl_rcd_component_reg_phys(&pdev->dev, dport);
490-
491-
put_device(&port->dev);
492-
493490
if (component_reg_phys == CXL_RESOURCE_NONE)
494491
return -ENXIO;
495492

0 commit comments

Comments
 (0)