Skip to content

Commit 04d3e54

Browse files
dtorgregkh
authored andcommitted
driver core: introduce device_set_driver() helper
In preparation to closing a race when reading driver pointer in dev_uevent() code, instead of setting device->driver pointer directly introduce device_set_driver() helper. Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com> Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> Link: https://lore.kernel.org/r/20250311052417.1846985-2-dmitry.torokhov@gmail.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent dc1771f commit 04d3e54

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

drivers/base/base.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ int driver_add_groups(const struct device_driver *drv, const struct attribute_gr
180180
void driver_remove_groups(const struct device_driver *drv, const struct attribute_group **groups);
181181
void device_driver_detach(struct device *dev);
182182

183+
static inline void device_set_driver(struct device *dev, const struct device_driver *drv)
184+
{
185+
// FIXME - this cast should not be needed "soon"
186+
dev->driver = (struct device_driver *)drv;
187+
}
188+
183189
int devres_release_all(struct device *dev);
184190
void device_block_probing(void);
185191
void device_unblock_probing(void);

drivers/base/core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3697,7 +3697,7 @@ int device_add(struct device *dev)
36973697
device_pm_remove(dev);
36983698
dpm_sysfs_remove(dev);
36993699
DPMError:
3700-
dev->driver = NULL;
3700+
device_set_driver(dev, NULL);
37013701
bus_remove_device(dev);
37023702
BusError:
37033703
device_remove_attrs(dev);

drivers/base/dd.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ static void device_unbind_cleanup(struct device *dev)
550550
arch_teardown_dma_ops(dev);
551551
kfree(dev->dma_range_map);
552552
dev->dma_range_map = NULL;
553-
dev->driver = NULL;
553+
device_set_driver(dev, NULL);
554554
dev_set_drvdata(dev, NULL);
555555
if (dev->pm_domain && dev->pm_domain->dismiss)
556556
dev->pm_domain->dismiss(dev);
@@ -629,8 +629,7 @@ static int really_probe(struct device *dev, const struct device_driver *drv)
629629
}
630630

631631
re_probe:
632-
// FIXME - this cast should not be needed "soon"
633-
dev->driver = (struct device_driver *)drv;
632+
device_set_driver(dev, drv);
634633

635634
/* If using pinctrl, bind pins now before probing */
636635
ret = pinctrl_bind_pins(dev);
@@ -1014,7 +1013,7 @@ static int __device_attach(struct device *dev, bool allow_async)
10141013
if (ret == 0)
10151014
ret = 1;
10161015
else {
1017-
dev->driver = NULL;
1016+
device_set_driver(dev, NULL);
10181017
ret = 0;
10191018
}
10201019
} else {

0 commit comments

Comments
 (0)