Skip to content

Commit 7096d04

Browse files
committed
of/device: Rework to use common platform_device_alloc() for allocating devices
The current code allocates and manages platform_devices created from the device tree manually. It also uses an unsafe shortcut for allocating the platform_device and the resource table at the same time. (which I added in the last rework; sorry). This patch refactors the code to use platform_device_alloc() for allocating new devices. This reduces the amount of custom code implemented by of_platform, eliminates the unsafe alloc trick, and has the side benefit of letting the platform_bus code manage freeing the device data and resources when the device is freed. Signed-off-by: Grant Likely <grant.likely@secretlab.ca> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org> Cc: Greg Kroah-Hartman <gregkh@suse.de> Cc: "David S. Miller" <davem@davemloft.net> Cc: Michal Simek <monstr@monstr.eu>
1 parent bda80da commit 7096d04

5 files changed

Lines changed: 32 additions & 44 deletions

File tree

‎arch/powerpc/kernel/ibmebus.c‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,10 @@ static int ibmebus_create_device(struct device_node *dn)
162162
dev->dev.bus = &ibmebus_bus_type;
163163
dev->dev.archdata.dma_ops = &ibmebus_dma_ops;
164164

165-
ret = of_device_register(dev);
166-
if (ret) {
167-
of_device_free(dev);
168-
return ret;
169-
}
170-
171-
return 0;
165+
ret = of_device_add(dev);
166+
if (ret)
167+
platform_device_put(dev);
168+
return ret;
172169
}
173170

174171
static int ibmebus_create_devices(const struct of_device_id *matches)

‎drivers/base/platform.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ static void platform_device_release(struct device *dev)
147147
struct platform_object *pa = container_of(dev, struct platform_object,
148148
pdev.dev);
149149

150+
of_device_node_put(&pa->pdev.dev);
150151
kfree(pa->pdev.dev.platform_data);
151152
kfree(pa->pdev.resource);
152153
kfree(pa);

‎drivers/of/device.c‎

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,29 +81,10 @@ struct device_attribute of_platform_device_attrs[] = {
8181
__ATTR_NULL
8282
};
8383

84-
/**
85-
* of_release_dev - free an of device structure when all users of it are finished.
86-
* @dev: device that's been disconnected
87-
*
88-
* Will be called only by the device core when all users of this of device are
89-
* done.
90-
*/
91-
void of_release_dev(struct device *dev)
92-
{
93-
struct platform_device *ofdev;
94-
95-
ofdev = to_platform_device(dev);
96-
of_node_put(ofdev->dev.of_node);
97-
kfree(ofdev);
98-
}
99-
EXPORT_SYMBOL(of_release_dev);
100-
101-
int of_device_register(struct platform_device *ofdev)
84+
int of_device_add(struct platform_device *ofdev)
10285
{
10386
BUG_ON(ofdev->dev.of_node == NULL);
10487

105-
device_initialize(&ofdev->dev);
106-
10788
/* name and id have to be set so that the platform bus doesn't get
10889
* confused on matching */
10990
ofdev->name = dev_name(&ofdev->dev);
@@ -117,6 +98,12 @@ int of_device_register(struct platform_device *ofdev)
11798

11899
return device_add(&ofdev->dev);
119100
}
101+
102+
int of_device_register(struct platform_device *pdev)
103+
{
104+
device_initialize(&pdev->dev);
105+
return of_device_add(pdev);
106+
}
120107
EXPORT_SYMBOL(of_device_register);
121108

122109
void of_device_unregister(struct platform_device *ofdev)

‎drivers/of/platform.c‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -587,20 +587,23 @@ struct platform_device *of_device_alloc(struct device_node *np,
587587
int rc, i, num_reg = 0, num_irq;
588588
struct resource *res, temp_res;
589589

590-
/* First count how many resources are needed */
590+
dev = platform_device_alloc("", -1);
591+
if (!dev)
592+
return NULL;
593+
594+
/* count the io and irq resources */
591595
while (of_address_to_resource(np, num_reg, &temp_res) == 0)
592596
num_reg++;
593597
num_irq = of_irq_count(np);
594598

595-
/* Allocate memory for both the struct device and the resource table */
596-
dev = kzalloc(sizeof(*dev) + (sizeof(*res) * (num_reg + num_irq)),
597-
GFP_KERNEL);
598-
if (!dev)
599-
return NULL;
600-
res = (struct resource *) &dev[1];
601-
602599
/* Populate the resource table */
603600
if (num_irq || num_reg) {
601+
res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
602+
if (!res) {
603+
platform_device_put(dev);
604+
return NULL;
605+
}
606+
604607
dev->num_resources = num_reg + num_irq;
605608
dev->resource = res;
606609
for (i = 0; i < num_reg; i++, res++) {
@@ -615,7 +618,6 @@ struct platform_device *of_device_alloc(struct device_node *np,
615618
dev->dev.dma_mask = &dev->archdata.dma_mask;
616619
#endif
617620
dev->dev.parent = parent;
618-
dev->dev.release = of_release_dev;
619621

620622
if (bus_id)
621623
dev_set_name(&dev->dev, "%s", bus_id);
@@ -653,8 +655,8 @@ struct platform_device *of_platform_device_create(struct device_node *np,
653655
* to do such, possibly using a device notifier
654656
*/
655657

656-
if (of_device_register(dev) != 0) {
657-
of_device_free(dev);
658+
if (of_device_add(dev) != 0) {
659+
platform_device_put(dev);
658660
return NULL;
659661
}
660662

‎include/linux/of_device.h‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,19 @@ static inline int of_driver_match_device(const struct device *dev,
2727
extern struct platform_device *of_dev_get(struct platform_device *dev);
2828
extern void of_dev_put(struct platform_device *dev);
2929

30+
extern int of_device_add(struct platform_device *pdev);
3031
extern int of_device_register(struct platform_device *ofdev);
3132
extern void of_device_unregister(struct platform_device *ofdev);
32-
extern void of_release_dev(struct device *dev);
33-
34-
static inline void of_device_free(struct platform_device *dev)
35-
{
36-
of_release_dev(&dev->dev);
37-
}
3833

3934
extern ssize_t of_device_get_modalias(struct device *dev,
4035
char *str, ssize_t len);
4136

4237
extern int of_device_uevent(struct device *dev, struct kobj_uevent_env *env);
4338

39+
static inline void of_device_node_put(struct device *dev)
40+
{
41+
of_node_put(dev->of_node);
42+
}
4443

4544
#else /* CONFIG_OF_DEVICE */
4645

@@ -56,6 +55,8 @@ static inline int of_device_uevent(struct device *dev,
5655
return -ENODEV;
5756
}
5857

58+
static inline void of_device_node_put(struct device *dev) { }
59+
5960
#endif /* CONFIG_OF_DEVICE */
6061

6162
#endif /* _LINUX_OF_DEVICE_H */

0 commit comments

Comments
 (0)