Skip to contentSkip to conversation

driver core: Fix possible memory leak in device_link_add()

PATCH In review Yang Yingliang 3 participants 4 messages Sep 30, 2021 – Oct 8, 2021 Driver core

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 driver core: Fix possible memory leak in device_link_add()

I got memory leak as follows:

unreferenced object 0xffff88801f0b2200 (size 64):
comm "i2c-lis2hh12-21", pid 5455, jiffies 4294944606 (age 15.224s) hex dump (first 32 bytes):

    72 65 67 75 6c 61 74 6f 72 3a 72 65 67 75 6c 61  regulator:regula
    74 6f 72 2e 30 2d 2d 69 32 63 3a 31 2d 30 30 31  tor.0--i2c:1-001
  backtrace:
    [<00000000bf5b0c3b>] __kmalloc_track_caller+0x19f/0x3a0
    [<0000000050da42d9>] kvasprintf+0xb5/0x150
    [<000000004bbbed13>] kvasprintf_const+0x60/0x190
    [<00000000cdac7480>] kobject_set_name_vargs+0x56/0x150
    [<00000000bf83f8e8>] dev_set_name+0xc0/0x100
    [<00000000cc1cf7e3>] device_link_add+0x6b4/0x17c0
    [<000000009db9faed>] _regulator_get+0x297/0x680
    [<00000000845e7f2b>] _devm_regulator_get+0x5b/0xe0
    [<000000003958ee25>] st_sensors_power_enable+0x71/0x1b0 [st_sensors]
    [<000000005f450f52>] st_accel_i2c_probe+0xd9/0x150 [st_accel_i2c]
    [<00000000b5f2ab33>] i2c_device_probe+0x4d8/0xbe0
    [<0000000070fb977b>] really_probe+0x299/0xc30
    [<0000000088e226ce>] __driver_probe_device+0x357/0x500
    [<00000000c21dda32>] driver_probe_device+0x4e/0x140
    [<000000004e650441>] __device_attach_driver+0x257/0x340
    [<00000000cf1891b8>] bus_for_each_drv+0x166/0x1e0

When device_register() returns an error, the name allocated in dev_set_name() will be leaked, the put_device() should be used instead of kfree() to give up the device reference, then the name will be freed in kobject_cleanup() and the references of consumer and supplier will be decreased in device_link_release_fn().

Fixes 287905e68dd2 ("driver core: Expose device link details in sysfs")
---
 drivers/base/core.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/base/core.c +1 -3
index e65dd803a453..4a123e58711f 100644
@@ -809,9 +809,7 @@ struct device_link *device_link_add(struct device *consumer,
 		     dev_bus_name(supplier), dev_name(supplier),
 		     dev_bus_name(consumer), dev_name(consumer));
 	if (device_register(&link->link_dev)) {
-		put_device(consumer);
-		put_device(supplier);
-		kfree(link);
+		put_device(&link->link_dev);
 		link = NULL;
 		goto out;
 	}
Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 54 lines · Yang Yingliang

I got memory leak as follows:

unreferenced object 0xffff88801f0b2200 (size 64):
comm "i2c-lis2hh12-21", pid 5455, jiffies 4294944606 (age 15.224s) hex dump (first 32 bytes):

    72 65 67 75 6c 61 74 6f 72 3a 72 65 67 75 6c 61  regulator:regula
    74 6f 72 2e 30 2d 2d 69 32 63 3a 31 2d 30 30 31  tor.0--i2c:1-001
  backtrace:
    [<00000000bf5b0c3b>] __kmalloc_track_caller+0x19f/0x3a0
    [<0000000050da42d9>] kvasprintf+0xb5/0x150
    [<000000004bbbed13>] kvasprintf_const+0x60/0x190
    [<00000000cdac7480>] kobject_set_name_vargs+0x56/0x150
    [<00000000bf83f8e8>] dev_set_name+0xc0/0x100
    [<00000000cc1cf7e3>] device_link_add+0x6b4/0x17c0
    [<000000009db9faed>] _regulator_get+0x297/0x680
    [<00000000845e7f2b>] _devm_regulator_get+0x5b/0xe0
    [<000000003958ee25>] st_sensors_power_enable+0x71/0x1b0 [st_sensors]
    [<000000005f450f52>] st_accel_i2c_probe+0xd9/0x150 [st_accel_i2c]
    [<00000000b5f2ab33>] i2c_device_probe+0x4d8/0xbe0
    [<0000000070fb977b>] really_probe+0x299/0xc30
    [<0000000088e226ce>] __driver_probe_device+0x357/0x500
    [<00000000c21dda32>] driver_probe_device+0x4e/0x140
    [<000000004e650441>] __device_attach_driver+0x257/0x340
    [<00000000cf1891b8>] bus_for_each_drv+0x166/0x1e0

When device_register() returns an error, the name allocated in dev_set_name() will be leaked, the put_device() should be used instead of kfree() to give up the device reference, then the name will be freed in kobject_cleanup() and the references of consumer and supplier will be decreased in device_link_release_fn().

Fixes 287905e68dd2 ("driver core: Expose device link details in sysfs")
---
 drivers/base/core.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/base/core.c +1 -4
index e65dd803a453..4a123e58711f 100644
@@ -809,9 +809,7 @@ struct device_link *device_link_add(struct device *consumer,
                     dev_bus_name(supplier), dev_name(supplier),
                     dev_bus_name(consumer), dev_name(consumer));
        if (device_register(&link->link_dev)) {
-               put_device(consumer);
-               put_device(supplier);
-               kfree(link);
+               put_device(&link->link_dev);
                link = NULL;
                goto out;
        }
--
Quoted 2 lines

2.25.1

Thanks for the fix!

Btw, can you also let us know why the device_register() is failing? It really shouldn't be failing.

-Saravana

#2 lore raw

Hi,

Quoted 58 lines · Saravana Kannan
Quoted 53 lines · Yang Yingliang

I got memory leak as follows:

unreferenced object 0xffff88801f0b2200 (size 64):

   comm "i2c-lis2hh12-21", pid 5455, jiffies 4294944606 (age 15.224s)
   hex dump (first 32 bytes):
     72 65 67 75 6c 61 74 6f 72 3a 72 65 67 75 6c 61  regulator:regula
     74 6f 72 2e 30 2d 2d 69 32 63 3a 31 2d 30 30 31  tor.0--i2c:1-001
   backtrace:
     [<00000000bf5b0c3b>] __kmalloc_track_caller+0x19f/0x3a0
     [<0000000050da42d9>] kvasprintf+0xb5/0x150
     [<000000004bbbed13>] kvasprintf_const+0x60/0x190
     [<00000000cdac7480>] kobject_set_name_vargs+0x56/0x150
     [<00000000bf83f8e8>] dev_set_name+0xc0/0x100
     [<00000000cc1cf7e3>] device_link_add+0x6b4/0x17c0
     [<000000009db9faed>] _regulator_get+0x297/0x680
     [<00000000845e7f2b>] _devm_regulator_get+0x5b/0xe0
     [<000000003958ee25>] st_sensors_power_enable+0x71/0x1b0 [st_sensors]
     [<000000005f450f52>] st_accel_i2c_probe+0xd9/0x150 [st_accel_i2c]
     [<00000000b5f2ab33>] i2c_device_probe+0x4d8/0xbe0
     [<0000000070fb977b>] really_probe+0x299/0xc30
     [<0000000088e226ce>] __driver_probe_device+0x357/0x500
     [<00000000c21dda32>] driver_probe_device+0x4e/0x140
     [<000000004e650441>] __device_attach_driver+0x257/0x340
     [<00000000cf1891b8>] bus_for_each_drv+0x166/0x1e0

When device_register() returns an error, the name allocated in dev_set_name() will be leaked, the put_device() should be used instead of kfree() to give up the device reference, then the name will be freed in kobject_cleanup() and the references of consumer and supplier will be decreased in device_link_release_fn().

Fixes 287905e68dd2 ("driver core: Expose device link details in sysfs")
---
  drivers/base/core.c | 4 +---
  1 file changed, 1 insertion(+), 3 deletions(-)
drivers/base/core.c +1 -4
index e65dd803a453..4a123e58711f 100644
@@ -809,9 +809,7 @@ struct device_link *device_link_add(struct device *consumer,
                      dev_bus_name(supplier), dev_name(supplier),
                      dev_bus_name(consumer), dev_name(consumer));
         if (device_register(&link->link_dev)) {
-               put_device(consumer);
-               put_device(supplier);
-               kfree(link);
+               put_device(&link->link_dev);
                 link = NULL;
                 goto out;
         }
--
Quoted 2 lines

2.25.1

Thanks for the fix!

Quoted 4 lines

Btw, can you also let us know why the device_register() is failing? It really shouldn't be failing.

I inject fault in device_add().

Quoted 3 lines

-Saravana
.

maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to Yang Yingliang
Quoted 26 lines · Yang Yingliang

I got memory leak as follows:

unreferenced object 0xffff88801f0b2200 (size 64):
comm "i2c-lis2hh12-21", pid 5455, jiffies 4294944606 (age 15.224s) hex dump (first 32 bytes):

    72 65 67 75 6c 61 74 6f 72 3a 72 65 67 75 6c 61  regulator:regula
    74 6f 72 2e 30 2d 2d 69 32 63 3a 31 2d 30 30 31  tor.0--i2c:1-001
  backtrace:
    [<00000000bf5b0c3b>] __kmalloc_track_caller+0x19f/0x3a0
    [<0000000050da42d9>] kvasprintf+0xb5/0x150
    [<000000004bbbed13>] kvasprintf_const+0x60/0x190
    [<00000000cdac7480>] kobject_set_name_vargs+0x56/0x150
    [<00000000bf83f8e8>] dev_set_name+0xc0/0x100
    [<00000000cc1cf7e3>] device_link_add+0x6b4/0x17c0
    [<000000009db9faed>] _regulator_get+0x297/0x680
    [<00000000845e7f2b>] _devm_regulator_get+0x5b/0xe0
    [<000000003958ee25>] st_sensors_power_enable+0x71/0x1b0 [st_sensors]
    [<000000005f450f52>] st_accel_i2c_probe+0xd9/0x150 [st_accel_i2c]
    [<00000000b5f2ab33>] i2c_device_probe+0x4d8/0xbe0
    [<0000000070fb977b>] really_probe+0x299/0xc30
    [<0000000088e226ce>] __driver_probe_device+0x357/0x500
    [<00000000c21dda32>] driver_probe_device+0x4e/0x140
    [<000000004e650441>] __device_attach_driver+0x257/0x340
    [<00000000cf1891b8>] bus_for_each_drv+0x166/0x1e0
Quoted 8 lines

When device_register() returns an error, the name allocated in dev_set_name() will be leaked, the put_device() should be used instead of kfree() to give up the device reference, then the name will be freed in kobject_cleanup() and the references of consumer and supplier will be decreased in device_link_release_fn().

Fixes 287905e68dd2 ("driver core: Expose device link details in sysfs")
Quoted 22 lines
---
 drivers/base/core.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/base/core.c +1 -4
index e65dd803a453..4a123e58711f 100644
@@ -809,9 +809,7 @@ struct device_link *device_link_add(struct device *consumer,
                     dev_bus_name(supplier), dev_name(supplier),
                     dev_bus_name(consumer), dev_name(consumer));
        if (device_register(&link->link_dev)) {
-               put_device(consumer);
-               put_device(supplier);
-               kfree(link);
+               put_device(&link->link_dev);
                link = NULL;
                goto out;
        }
--

2.25.1