Skip to content

Commit 9ad3072

Browse files
robherringgregkh
authored andcommitted
driver core: Refactor multiple copies of device cleanup
There are 3 copies of the same device cleanup code used for probe failure, testing re-probing, and device unbinding. Changes to this code often miss at least one of the copies of the code. See commits d0243bb ("drivers core: Free dma_range_map when driver probe failed") and d8f7a54 ("driver core: Free DMA range map when device is released") for example. Let's refactor the code to its own function. Signed-off-by: Rob Herring <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 5bff963 commit 9ad3072

File tree

1 file changed

+16
-30
lines changed

1 file changed

+16
-30
lines changed

drivers/base/dd.c

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,19 @@ static ssize_t state_synced_show(struct device *dev,
506506
}
507507
static DEVICE_ATTR_RO(state_synced);
508508

509+
static void device_unbind_cleanup(struct device *dev)
510+
{
511+
devres_release_all(dev);
512+
arch_teardown_dma_ops(dev);
513+
kfree(dev->dma_range_map);
514+
dev->dma_range_map = NULL;
515+
dev->driver = NULL;
516+
dev_set_drvdata(dev, NULL);
517+
if (dev->pm_domain && dev->pm_domain->dismiss)
518+
dev->pm_domain->dismiss(dev);
519+
pm_runtime_reinit(dev);
520+
dev_pm_set_driver_flags(dev, 0);
521+
}
509522

510523
static int call_driver_probe(struct device *dev, struct device_driver *drv)
511524
{
@@ -628,16 +641,8 @@ static int really_probe(struct device *dev, struct device_driver *drv)
628641
else if (drv->remove)
629642
drv->remove(dev);
630643

631-
devres_release_all(dev);
632-
arch_teardown_dma_ops(dev);
633-
kfree(dev->dma_range_map);
634-
dev->dma_range_map = NULL;
635644
driver_sysfs_remove(dev);
636-
dev->driver = NULL;
637-
dev_set_drvdata(dev, NULL);
638-
if (dev->pm_domain && dev->pm_domain->dismiss)
639-
dev->pm_domain->dismiss(dev);
640-
pm_runtime_reinit(dev);
645+
device_unbind_cleanup(dev);
641646

642647
goto re_probe;
643648
}
@@ -667,16 +672,7 @@ static int really_probe(struct device *dev, struct device_driver *drv)
667672
BUS_NOTIFY_DRIVER_NOT_BOUND, dev);
668673
pinctrl_bind_failed:
669674
device_links_no_driver(dev);
670-
devres_release_all(dev);
671-
arch_teardown_dma_ops(dev);
672-
kfree(dev->dma_range_map);
673-
dev->dma_range_map = NULL;
674-
dev->driver = NULL;
675-
dev_set_drvdata(dev, NULL);
676-
if (dev->pm_domain && dev->pm_domain->dismiss)
677-
dev->pm_domain->dismiss(dev);
678-
pm_runtime_reinit(dev);
679-
dev_pm_set_driver_flags(dev, 0);
675+
device_unbind_cleanup(dev);
680676
done:
681677
return ret;
682678
}
@@ -1209,17 +1205,7 @@ static void __device_release_driver(struct device *dev, struct device *parent)
12091205
drv->remove(dev);
12101206

12111207
device_links_driver_cleanup(dev);
1212-
1213-
devres_release_all(dev);
1214-
arch_teardown_dma_ops(dev);
1215-
kfree(dev->dma_range_map);
1216-
dev->dma_range_map = NULL;
1217-
dev->driver = NULL;
1218-
dev_set_drvdata(dev, NULL);
1219-
if (dev->pm_domain && dev->pm_domain->dismiss)
1220-
dev->pm_domain->dismiss(dev);
1221-
pm_runtime_reinit(dev);
1222-
dev_pm_set_driver_flags(dev, 0);
1208+
device_unbind_cleanup(dev);
12231209

12241210
klist_remove(&dev->p->knode_driver);
12251211
device_pm_check_callbacks(dev);

0 commit comments

Comments
 (0)