Skip to contentSkip to conversation

i2c: core: Fix possible memleak in i2c_new_client_device()

PATCH Superseded Yang Yingliang 2 participants 3 messages Oct 15, 2021 – Oct 18, 2021

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: core: Fix possible memleak in i2c_new_client_device()

I got memory leak as follows when doing fault injection test:

unreferenced object 0xffff888014aec078 (size 8):
comm "xrun", pid 356, jiffies 4294910619 (age 16.332s) hex dump (first 8 bytes):

    31 2d 30 30 31 63 00 00                          1-001c..
  backtrace:
    [<00000000eb56c0a9>] __kmalloc_track_caller+0x1a6/0x300
    [<000000000b220ea3>] kvasprintf+0xad/0x140
    [<00000000b83203e5>] kvasprintf_const+0x62/0x190
    [<000000002a5eab37>] kobject_set_name_vargs+0x56/0x140
    [<00000000300ac279>] dev_set_name+0xb0/0xe0
    [<00000000b66ebd6f>] i2c_new_client_device+0x7e4/0x9a0

In error path after calling i2c_dev_set_name(), the put_device() should be used to give up the device reference, then the name allocated in dev_set_name() will be freed in kobject_cleanup().

---
 drivers/i2c/i2c-core-base.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
drivers/i2c/i2c-core-base.c +7 -6
index 54964fbe3f03..190d4fd5e594 100644
@@ -1047,8 +1047,6 @@ i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *inf
 	client->dev.of_node = of_node_get(info->of_node);
 	client->dev.fwnode = info->fwnode;
 
-	i2c_dev_set_name(adap, client, info);
-
 	if (info->swnode) {
 		status = device_add_software_node(&client->dev, info->swnode);
 		if (status) {
@@ -1059,17 +1057,20 @@ i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *inf
 		}
 	}
 
+	i2c_dev_set_name(adap, client, info);
 	status = device_register(&client->dev);
-	if (status)
-		goto out_remove_swnode;
+	if (status) {
+		device_remove_software_node(&client->dev);
+		of_node_put(info->of_node);
+		put_device(&client->dev);
+		return ERR_PTR(status);
+	}
 
 	dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
 		client->name, dev_name(&client->dev));
 
 	return client;
 
-out_remove_swnode:
-	device_remove_software_node(&client->dev);
 out_err_put_of_node:
 	of_node_put(info->of_node);
 out_err:
Signature · 1 line
2.25.1
#1 lore raw
Quoted 4 lines

In error path after calling i2c_dev_set_name(), the put_device() should be used to give up the device reference, then the name allocated in dev_set_name() will be freed in kobject_cleanup().

I don't see it. dev_set_name does not call device_get, so why should we call device_put on failure? No other user of dev_set_name seems to do this. So, if this is an imbalance, where does the unmatched get_device really come from?

Quoted 48 lines
---
 drivers/i2c/i2c-core-base.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
drivers/i2c/i2c-core-base.c +7 -6
index 54964fbe3f03..190d4fd5e594 100644
@@ -1047,8 +1047,6 @@ i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *inf
 	client->dev.of_node = of_node_get(info->of_node);
 	client->dev.fwnode = info->fwnode;
 
-	i2c_dev_set_name(adap, client, info);
-
 	if (info->swnode) {
 		status = device_add_software_node(&client->dev, info->swnode);
 		if (status) {
@@ -1059,17 +1057,20 @@ i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *inf
 		}
 	}
 
+	i2c_dev_set_name(adap, client, info);
 	status = device_register(&client->dev);
-	if (status)
-		goto out_remove_swnode;
+	if (status) {
+		device_remove_software_node(&client->dev);
+		of_node_put(info->of_node);
+		put_device(&client->dev);
+		return ERR_PTR(status);
+	}
 
 	dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
 		client->name, dev_name(&client->dev));
 
 	return client;
 
-out_remove_swnode:
-	device_remove_software_node(&client->dev);
 out_err_put_of_node:
 	of_node_put(info->of_node);
 out_err:
Signature · 1 line
2.25.1
#2 lore raw

Hi,

Quoted 7 lines · Wolfram Sang
Quoted 3 lines

In error path after calling i2c_dev_set_name(), the put_device() should be used to give up the device reference, then the name allocated in dev_set_name() will be freed in kobject_cleanup().

I don't see it. dev_set_name does not call device_get, so why should we call device_put on failure? No other user of dev_set_name seems to do this. So, if this is an imbalance, where does the unmatched get_device really come from?

The reference is initialized in device_initialize() called in device_register(), if device_register() fails, the 'kobj->name' is leaked.

Thanks,
Yang

Quoted 48 lines
Quoted 48 lines
---
  drivers/i2c/i2c-core-base.c | 13 +++++++------
  1 file changed, 7 insertions(+), 6 deletions(-)
drivers/i2c/i2c-core-base.c +7 -6
index 54964fbe3f03..190d4fd5e594 100644
@@ -1047,8 +1047,6 @@ i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *inf
  	client->dev.of_node = of_node_get(info->of_node);
  	client->dev.fwnode = info->fwnode;
 
-	i2c_dev_set_name(adap, client, info);
-
  	if (info->swnode) {
  		status = device_add_software_node(&client->dev, info->swnode);
  		if (status) {
@@ -1059,17 +1057,20 @@ i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *inf
  		}
  	}
 
+	i2c_dev_set_name(adap, client, info);
  	status = device_register(&client->dev);
-	if (status)
-		goto out_remove_swnode;
+	if (status) {
+		device_remove_software_node(&client->dev);
+		of_node_put(info->of_node);
+		put_device(&client->dev);
+		return ERR_PTR(status);
+	}
 
  	dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
  		client->name, dev_name(&client->dev));
 
  	return client;
 
-out_remove_swnode:
-	device_remove_software_node(&client->dev);
  out_err_put_of_node:
  	of_node_put(info->of_node);
  out_err:
Signature · 1 line
2.25.1