[PATCH v19 1/7] device_core: rely on put_device to free dev->p
From: Tarun Sahu
Date: Thu Jul 16 2026 - 22:42:18 EST
device_add allocate private_data for device and assigns to
dev->p. If device_add fails in later steps of the function,
it cleans up this dev->p which is not necessary because In
the next call, put_device free it anyway (if reference to
the device is 0 which will be unless someone concurrently
get the reference to this device).
This avoids unnecessary races introduced in system. After device
is added in device_kset->list by device_add and later steps in the
device_add function failures occur, it will free dev->p manually,
while in between there might be a user of device_kset->list will
take reference to the device just added by device_add. and might
try to access dev->p. So relying on put_device to free dev->p
prevents such problem.
Signed-off-by: Tarun Sahu <tarunsahu@xxxxxxxxxx>
Signed-off-by: David Jeffery <djeffery@xxxxxxxxxx>
---
drivers/base/core.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/drivers/base/core.c b/drivers/base/core.c
index 4d026682944f..76ba02c26aa5 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -2614,6 +2614,7 @@ static void device_release(struct kobject *kobj)
struct device *dev = kobj_to_dev(kobj);
struct device_private *p = dev->p;
+ dev->p = NULL;
/*
* Some platform devices are driven without driver attached
* and managed resources may have been acquired. Make sure
@@ -3824,8 +3825,6 @@ int device_add(struct device *dev)
parent_error:
put_device(parent);
name_error:
- kfree(dev->p);
- dev->p = NULL;
goto done;
}
EXPORT_SYMBOL_GPL(device_add);
--
2.55.0.229.g6434b31f56-goog