Convert manual kfree(dev) to put_device()
Subsystems (3)
v4 cover Convert manual kfree(dev) to put_device()
Hello,
As per the kernel-doc guidelines, Devices that are tried to register with device_register or to add using device_add, must use put_device on their failure path (not kfree). This series contains 3 patches for updating such instances. Same for kobject_init_and_add or kobject_add, call kobject_put instead of kfree in case of error.
V4 <- V3
1. Added Fixes tags
2. update commit msg as per the suggestion on v3
V3 <- V2
1. Updated the commit msg of each patch to mention about the kernel-doc guidelines.
V2 <- V1
1. Removed patch [1] as it is part of another series [2] already.
[1]: https://lore.kernel.org/all/[email protected]/
[2]: https://lore.kernel.org/all/[email protected]/
Tarun Sahu (3):
ARM: locomo: Fix device reference leak on registration failure
firmware: edd: Fix kobject reference leak on registration failure powerpc/ps3: use put_device() on device_register() failure in
ps3_system_bus_device_register
arch/arm/common/locomo.c | 13 ++-- arch/powerpc/platforms/ps3/device-init.c | 83 ++++++++++++++---------- arch/powerpc/platforms/ps3/system-bus.c | 2 + drivers/firmware/edd.c | 2 +- 4 files changed, 59 insertions(+), 41 deletions(-)
base-commit: cee9395acd8043be0644b25c34bfa86623f2b935
Signature · 1 line
2.55.0.979.g7e5102b832-goog
PATCH v4 1/3 ARM: locomo: Fix device reference leak on registration failure
As per device_register() kernel-doc, calling kfree() directly on error bypasses reference counting and skips the device release callback, leaking the reference. Even in case of device_register() failure, Calling the put_device is advised.
Fix this by calling put_device() on device_registration failure instead of kfree().
--- arch/arm/common/locomo.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
arch/arm/common/locomo.c +6 -7
@@ -223,10 +223,8 @@ locomo_init_one_child(struct locomo *lchip, struct locomo_dev_info *info)int ret; dev = kzalloc_obj(struct locomo_dev); -if (!dev) { - ret = -ENOMEM ; - goto out; - } +if (!dev) +return -ENOMEM ;/* * If the parent device has a DMA mask associated with it,
@@ -255,10 +253,11 @@ locomo_init_one_child(struct locomo *lchip, struct locomo_dev_info *info) ret =device_register (&dev->dev);if (ret) { - out: -kfree (dev); +put_device (&dev->dev); +return ret; } -return ret; + +return 0 ; }#ifdef CONFIG_PM
Signature · 1 line
2.55.0.979.g7e5102b832-goog
PATCH v4 2/3 firmware: edd: Fix kobject reference leak on registration failure
In reply to Tarun SahuPer kobject_init_and_add() kernel-doc, calling kfree() directly on error bypasses reference counting and skips the kobject's release callback, leaking the reference.
Use kobject_put() instead of kfree() on registration failure to fix this.
--- drivers/firmware/edd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/edd.c +1 -1
@@ -748,7 +748,7 @@ edd_init(void) rc =edd_device_register (edev, i);if (rc) { -kfree (edev); +kobject_put (&edev->kobj); goto out; } edd_devices[i] = edev;
Signature · 1 line
2.55.0.979.g7e5102b832-goog
PATCH v4 3/3 powerpc/ps3: use put_device() on device_register() failure in ps3_system_bus_device_register
In reply to Tarun SahuAs per the kernel documentation of device_register() function, it is important to call put_device even if device_register returns an error.
To follow this guidelines and properly release the resources after device_register() failure, call put_device() instead of kfree()
Also there are in-function-defined struct layout which make struct device (core) to be child of layout-child's member (layout.dev.core). Also definition of struct layout is not unique across functions in the driver. To be able to free struct layout's dynamic allocation via put_device we need to make sure that the core's release function must call the free on parent of core and the parent must be at the location 0 of the struct layout which will inherently free struct layout. This is to not complicate the code and keep it as it currently implemented. To check the location of parent at 0 of struct layout, I have added BUILD_BUG_ON.
--- arch/powerpc/platforms/ps3/device-init.c | 83 ++++++++++++++---------- arch/powerpc/platforms/ps3/system-bus.c | 2 + 2 files changed, 52 insertions(+), 33 deletions(-)
arch/powerpc/platforms/ps3/device-init.c +50 -33
@@ -90,14 +90,12 @@ static int __init ps3_register_lpm_devices(void)if (result) {pr_debug ("%s:%d ps3_system_bus_device_register failed\n" , __func__, __LINE__); - goto fail_register; +return result; }pr_debug (" <- %s:%d\n" , __func__, __LINE__);return 0 ; - -fail_register: fail_rights: fail_read_repo:kfree (dev);
@@ -121,6 +119,12 @@ static int __init ps3_setup_gelic_device(struct ps3_dma_region d_region; } *p; +/* +* ps3_system_bus_release_device() calls kfree(&p->dev). +* dev must be at offset 0 so kfree() frees outer p. +*/ +BUILD_BUG_ON (offsetof(struct layout , dev) !=0 ); +pr_debug (" -> %s:%d\n" , __func__, __LINE__);BUG_ON (repo->bus_type !=PS3_BUS_TYPE_SB );
@@ -164,13 +168,12 @@ static int __init ps3_setup_gelic_device(if (result) {pr_debug ("%s:%d ps3_system_bus_device_register failed\n" , __func__, __LINE__); - goto fail_device_register; +return result; }pr_debug (" <- %s:%d\n" , __func__, __LINE__);return result; -fail_device_register: fail_dma_init: fail_find_interrupt:kfree (p);
@@ -192,6 +195,12 @@ static int __init ps3_setup_uhc_device(u64 bus_addr;u64 len; +/* +* ps3_system_bus_release_device() calls kfree(&p->dev). +* dev must be at offset 0 so kfree() frees outer p. +*/ +BUILD_BUG_ON (offsetof(struct layout , dev) !=0 ); +pr_debug (" -> %s:%d\n" , __func__, __LINE__);BUG_ON (repo->bus_type !=PS3_BUS_TYPE_SB );
@@ -252,13 +261,12 @@ static int __init ps3_setup_uhc_device(if (result) {pr_debug ("%s:%d ps3_system_bus_device_register failed\n" , __func__, __LINE__); - goto fail_device_register; +return result; }pr_debug (" <- %s:%d\n" , __func__, __LINE__);return result; -fail_device_register: fail_mmio_init: fail_dma_init: fail_find_reg:
@@ -291,6 +299,12 @@ static int __init ps3_setup_vuart_device(enum ps3_match_id match_id,struct ps3_system_bus_device dev; } *p; +/* +* ps3_system_bus_release_device() calls kfree(&p->dev). +* dev must be at offset 0 so kfree() frees outer p. +*/ +BUILD_BUG_ON (offsetof(struct layout , dev) !=0 ); +pr_debug (" -> %s:%d: match_id %u, port %u\n" , __func__, __LINE__, match_id, port_number);
@@ -308,15 +322,10 @@ static int __init ps3_setup_vuart_device(enum ps3_match_id match_id,if (result) {pr_debug ("%s:%d ps3_system_bus_device_register failed\n" , __func__, __LINE__); - goto fail_device_register; +return result; }pr_debug (" <- %s:%d\n" , __func__, __LINE__);return 0 ; - -fail_device_register: -kfree (p); -pr_debug (" <- %s:%d fail\n" , __func__, __LINE__); -return result; } static int ps3_setup_storage_dev(const struct ps3_repository_device *repo,
@@ -327,6 +336,12 @@ static int ps3_setup_storage_dev(const struct ps3_repository_device *repo,u64 port, blk_size, num_blocks;unsigned int num_regions, i; +/* +* ps3_system_bus_release_device() calls kfree(&p->sbd). +* sbd must be at offset 0 so kfree() frees outer p. +*/ +BUILD_BUG_ON (offsetof(struct ps3_storage_device , sbd) !=0 ); +pr_debug (" -> %s:%u: match_id %u\n" , __func__, __LINE__, match_id); result = ps3_repository_read_stor_dev_info(repo->bus_index,
@@ -395,13 +410,12 @@ static int ps3_setup_storage_dev(const struct ps3_repository_device *repo,if (result) {pr_debug ("%s:%u ps3_system_bus_device_register failed\n" , __func__, __LINE__); - goto fail_device_register; +return result; }pr_debug (" <- %s:%u\n" , __func__, __LINE__);return 0 ; -fail_device_register: fail_read_region: fail_find_interrupt:kfree (p);
@@ -445,6 +459,12 @@ static int __init ps3_register_sound_devices(void)struct ps3_mmio_region m_region; } *p; +/* +* ps3_system_bus_release_device() calls kfree(&p->dev). +* dev must be at offset 0 so kfree() frees outer p. +*/ +BUILD_BUG_ON (offsetof(struct layout , dev) !=0 ); +pr_debug (" -> %s:%d\n" , __func__, __LINE__); p =kzalloc_obj (*p);
@@ -461,15 +481,10 @@ static int __init ps3_register_sound_devices(void)if (result) {pr_debug ("%s:%d ps3_system_bus_device_register failed\n" , __func__, __LINE__); - goto fail_device_register; +return result; }pr_debug (" <- %s:%d\n" , __func__, __LINE__);return 0 ; - -fail_device_register: -kfree (p); -pr_debug (" <- %s:%d failed\n" , __func__, __LINE__); -return result; }static int __init ps3_register_graphics_devices (void )
@@ -479,6 +494,12 @@ static int __init ps3_register_graphics_devices(void)struct ps3_system_bus_device dev; } *p; +/* +* ps3_system_bus_release_device() calls kfree(&p->dev). +* dev must be at offset 0 so kfree() frees outer p. +*/ +BUILD_BUG_ON (offsetof(struct layout , dev) !=0 ); +pr_debug (" -> %s:%d\n" , __func__, __LINE__); p = kzalloc_obj(struct layout);
@@ -495,16 +516,11 @@ static int __init ps3_register_graphics_devices(void)if (result) {pr_debug ("%s:%d ps3_system_bus_device_register failed\n" , __func__, __LINE__); - goto fail_device_register; +return result; }pr_debug (" <- %s:%d\n" , __func__, __LINE__);return 0 ; - -fail_device_register: -kfree (p); -pr_debug (" <- %s:%d failed\n" , __func__, __LINE__); -return result; }static int __init ps3_register_ramdisk_device (void )
@@ -514,6 +530,12 @@ static int __init ps3_register_ramdisk_device(void)struct ps3_system_bus_device dev; } *p; +/* +* ps3_system_bus_release_device() calls kfree(&p->dev). +* dev must be at offset 0 so kfree() frees outer p. +*/ +BUILD_BUG_ON (offsetof(struct layout , dev) !=0 ); +pr_debug (" -> %s:%d\n" , __func__, __LINE__); p = kzalloc_obj(struct layout);
@@ -530,16 +552,11 @@ static int __init ps3_register_ramdisk_device(void)if (result) {pr_debug ("%s:%d ps3_system_bus_device_register failed\n" , __func__, __LINE__); - goto fail_device_register; +return result; }pr_debug (" <- %s:%d\n" , __func__, __LINE__);return 0 ; - -fail_device_register: -kfree (p); -pr_debug (" <- %s:%d failed\n" , __func__, __LINE__); -return result; }/**
arch/powerpc/platforms/ps3/system-bus.c +2 -0
@@ -774,6 +774,8 @@ int ps3_system_bus_device_register(struct ps3_system_bus_device *dev)pr_debug ("%s:%d add %s\n" , __func__, __LINE__,dev_name (&dev->core)); result =device_register (&dev->core); +if (result) +put_device (&dev->core);return result; }
Signature · 1 line
2.55.0.979.g7e5102b832-goog
Hello, This is the gentle remind for picking up this series.
Please, let me know if it needs any improvements.
~Tarun
Quoted 38 lines · Tarun Sahu
Hello,
As per the kernel-doc guidelines, Devices that are tried to register with
device_registeror to add usingdevice_add, must useput_deviceon their failure path (not kfree). This series contains 3 patches for updating such instances. Same forkobject_init_and_addorkobject_add, callkobject_putinstead of kfree in case of error.V4 <- V3
1. Added Fixes tags
2. update commit msg as per the suggestion on v3V3 <- V2
1. Updated the commit msg of each patch to mention about the kernel-doc guidelines.V2 <- V1
1. Removed patch [1] as it is part of another series [2] already.[1]: https://lore.kernel.org/all/[email protected]/
[2]: https://lore.kernel.org/all/[email protected]/Tarun Sahu (3):
ARM: locomo: Fix device reference leak on registration failure
firmware: edd: Fix kobject reference leak on registration failure powerpc/ps3: useput_device()ondevice_register()failure in
ps3_system_bus_device_registerarch/arm/common/locomo.c | 13 ++-- arch/powerpc/platforms/ps3/device-init.c | 83 ++++++++++++++---------- arch/powerpc/platforms/ps3/system-bus.c | 2 + drivers/firmware/edd.c | 2 +- 4 files changed, 59 insertions(+), 41 deletions(-)base-commit:
cee9395acd8043be0644b25c34bfa86623f2b935Signature · 1 line
2.55.0.979.g7e5102b832-goog