Skip to contentSkip to conversation

driver core: use put_device() instead of kfree()

PATCH New Arvind Yadav 1 participant 5 messages Mar 11, 2018 Driver core

lore
Share & tools
lore
#0 lore raw

cover driver core: use put_device() instead of kfree()

Never directly free @dev after calling device_register(), even if it returned an error! Always use put_device() to give up the reference initialized.

Arvind Yadav (4):
[PATCH 1/4] base: soc: use put_device() instead of kfree() [PATCH 2/4] driver core: platform: use put_device() if device_register fail [PATCH 3/4] driver core: node: use put_device() if device_register fail [PATCH 4/4] driver core: cpu: use put_device() if device_register fail

 drivers/base/cpu.c      | 4 +++-
 drivers/base/node.c     | 4 +++-
 drivers/base/platform.c | 4 +++-
 drivers/base/soc.c      | 2 ++
 4 files changed, 11 insertions(+), 3 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/4 base: soc: use put_device() instead of kfree()

Never directly free @dev after calling device_register(), even if it returned an error! Always use put_device() to give up the reference initialized.

---
 drivers/base/soc.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/base/soc.c +2 -0
index 4e80f48..10b280f 100644
@@ -150,6 +150,8 @@ struct soc_device *soc_device_register(struct soc_device_attribute *soc_dev_attr
 
 out3:
 	ida_simple_remove(&soc_ida, soc_dev->soc_dev_num);
+	put_device(&soc_dev->dev);
+	soc_dev = NULL;
 out2:
 	kfree(soc_dev);
 out1:
Signature · 1 line
2.7.4
#2 lore raw

PATCH 2/4 driver core: platform: use put_device() if device_register fail

In reply to Arvind Yadav

if device_register() returned an error! Always use put_device() to give up the reference initialized.

---
 drivers/base/platform.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/base/platform.c +3 -1
index f1bf7b3..8075ddc 100644
@@ -1153,8 +1153,10 @@ int __init platform_bus_init(void)
 	early_platform_cleanup();
 
 	error = device_register(&platform_bus);
-	if (error)
+	if (error) {
+		put_device(&platform_bus);
 		return error;
+	}
 	error =  bus_register(&platform_bus_type);
 	if (error)
 		device_unregister(&platform_bus);
Signature · 1 line
2.7.4
#3 lore raw

PATCH 3/4 driver core: node: use put_device() if device_register fail

In reply to Arvind Yadav

if device_register() returned an error! Always use put_device() to give up the reference initialized.

---
 drivers/base/node.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/base/node.c +3 -1
index ee090ab..c5f81fc 100644
@@ -315,7 +315,9 @@ static int register_node(struct node *node, int num)
 	node->dev.groups = node_dev_groups;
 	error = device_register(&node->dev);
 
-	if (!error){
+	if (error)
+		put_device(&node->dev);
+	else {
 		hugetlb_register_node(node);
 
 		compaction_register_node(node);
Signature · 1 line
2.7.4
#4 lore raw

PATCH 4/4 driver core: cpu: use put_device() if device_register fail

In reply to Arvind Yadav

if device_register() returned an error! Always use put_device() to give up the reference initialized.

---
 drivers/base/cpu.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/base/cpu.c +3 -1
index d21a2d9..2da998b 100644
@@ -382,8 +382,10 @@ int register_cpu(struct cpu *cpu, int num)
 	if (cpu->hotpluggable)
 		cpu->dev.groups = hotplugable_cpu_attr_groups;
 	error = device_register(&cpu->dev);
-	if (error)
+	if (error) {
+		put_device(&cpu->dev);
 		return error;
+	}
 
 	per_cpu(cpu_sys_devices, num) = &cpu->dev;
 	register_cpu_under_node(num, cpu_to_node(num));
Signature · 1 line
2.7.4