All of
 help / color / mirror / Atom feed
* [PATCH] of: Deep-copy names of platform devices
@ 2014-08-13  0:57 Stepan Moskovchenko
  2014-08-13  1:46 ` Stephen Boyd
  0 siblings, 1 reply; 10+ messages in thread
From: Stepan Moskovchenko @ 2014-08-13  0:57 UTC (permalink / raw)
  To: grant.likely, robh+dt, devicetree; +Cc: linux-kernel, linux-arm-msm, stepanm

When we parse the device tree and allocate platform
devices, the 'name' of the newly-created platform_device
is set to point to the 'name' field of the 'struct device'
embedded within the platform_device. This is dangerous,
because the name of the 'struct device' is dynamically
allocated. Drivers may call dev_set_name() on the device,
which will free and reallocate the name of the device,
leaving the 'name' of the platform_device pointing to the
now-freed memory.

Furthermore, if the dev_set_name() call is made from a
driver's probe() function and a subsequent request results
in probe deferral, the dangling 'name' reference may lead
to the device being re-probed using the wrong driver.

To mitigate these scenarios, we use kstrdup to perform a
deep copy of the device name when assigning the name of the
platform_device, so that the platform_device name is
unaffected by any calls to dev_set_name() that might made
by drivers to rename the embedded 'struct device'.

Signed-off-by: Stepan Moskovchenko <>
This is technically a 'v2' patch, but it looks like I used
an old version of MAINTAINERS, so I'll just re-send this
as a new patch to avoid confusion for people who missed the

I suppose creating a 'pdev_set_name' API may seem like
another possibility, but I feel that and
have two different meanings. One is used for device/driver
binding purposes, whereas the other serves a more general
identification purpose, and is used for things like sysfs.
Drivers might want to change while leaving the alone. I guess yet another possibility would be
to prohibit calling dev_set_name() on devices created from
device tree, but a driver does not necessarily know how a
given platform_device was allocated.


 drivers/of/device.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/of/device.c b/drivers/of/device.c
index f685e55..3e116f6 100644
--- a/drivers/of/device.c
+++ b/drivers/of/device.c
@@ -54,7 +54,7 @@ int of_device_add(struct platform_device *ofdev)

 	/* name and id have to be set so that the platform bus doesn't get
 	 * confused on matching */
-	ofdev->name = dev_name(&ofdev->dev);
+	ofdev->name = kstrdup(dev_name(&ofdev->dev), GFP_KERNEL);
 	ofdev->id = -1;

 	/* device_add will assume that this device is on the same node as
@@ -76,6 +76,7 @@ EXPORT_SYMBOL(of_device_register);
 void of_device_unregister(struct platform_device *ofdev)
+	kfree(ofdev->name);

The Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
hosted by The Linux Foundation

^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2014-08-16 18:30 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-13  0:57 [PATCH] of: Deep-copy names of platform devices Stepan Moskovchenko
2014-08-13  1:46 ` Stephen Boyd
2014-08-13  2:30   ` [PATCH v2] " Stepan Moskovchenko
2014-08-15 16:38     ` Rob Herring
2014-08-16  4:19       ` Greg Kroah-Hartman
2014-08-16 18:29       ` Grant Likely
2014-08-15 10:45   ` [PATCH] " Grant Likely
2014-08-15 10:52     ` Grant Likely
     [not found]       ` <>
2014-08-15 11:01         ` Grant Likely
2014-08-15 11:01           ` Grant Likely

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.