Fix kobject error path memleaks
cover Fix kobject error path memleaks
Hi,
Is it ok to send patches during the merge window? Applies on top of Linus' mainline tag: v5.1, happy to rebase if there are conflicts.
While auditing kobject_init_and_add() calls throughout the kernel it was found that btrfs potentially has a couple of memleaks in the error path code for kobject_init_and_add().
Failing calls to kobject_init_and_add() should be followed by a call to kobject_put() since kobject_init_and_add() always calls kobject_init().
Of note, adding kobject_put() causes the release method to be called if kobject_init_and_add() fails. For patch #1 this means we don't have to manually free the space_info or call percpu_counter_destroy() since these are both done by the release method. In the second patch, I believe the added call to kobject_put() fits in with the fs_devices lifecycle assumptions of open_ctree() but please could you review since I am new to this code.
CC'ing the kobject maintainers/reviewers also.
Thanks,
Tobin.
Tobin C. Harding (2):
fs: btrfs: Fix error path kobject memory leak
fs: btrfs: Don't leak memory when failing add fsid
fs/btrfs/extent-tree.c | 3 +-- fs/btrfs/sysfs.c | 7 ++++++- 2 files changed, 7 insertions(+), 3 deletions(-)
Signature · 1 line
2.21.0
PATCH 1/2 fs: btrfs: Fix error path kobject memory leak
If a call to kobject_init_and_add() fails we must call kobject_put() otherwise we leak memory.
Calling kobject_put() when kobject_init_and_add() fails drops the refcount back to 0 and calls the ktype release method.
Add call to kobject_put() in the error path of call to kobject_init_and_add().
--- fs/btrfs/extent-tree.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
fs/btrfs/extent-tree.c +1 -2
@@ -3981,8 +3981,7 @@ static int create_space_info(struct btrfs_fs_info *info, u64 flags) info->space_info_kobj,"%s" , alloc_name(space_info->flags));if (ret) { -percpu_counter_destroy (&space_info->total_bytes_pinned); -kfree (space_info); +kobject_put (&space_info->kobj);return ret; }
Signature · 1 line
2.21.0
Re: [PATCH 1/2] fs: btrfs: Fix error path kobject memory leak
Quoted 19 lines · Tobin C. Harding
If a call to
kobject_init_and_add()fails we must callkobject_put()otherwise we leak memory.Calling
kobject_put()whenkobject_init_and_add()fails drops the refcount back to 0 and calls the ktype release method.Add call to
kobject_put()in the error path of call tokobject_init_and_add().Signed-off-by Tobin C. Harding <[email protected]>--- fs/btrfs/extent-tree.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)fs/btrfs/extent-tree.c +0
-0
fs/btrfs/extent-tree.c static int create_space_info(struct btrfs_fs_info *info, u64 flags) · line 3984
Quoted 6 lines
fs/btrfs/extent-tree.c +1
-2@@ -3981,8 +3981,7 @@ static int create_space_info(struct btrfs_fs_info *info, u64 flags) info->space_info_kobj,"%s" , alloc_name(space_info->flags));if (ret) { -percpu_counter_destroy (&space_info->total_bytes_pinned); -kfree (space_info); +kobject_put (&space_info->kobj);
If you are only fixing kobject-related code then why do you delete correct code as well? percpu_counter_Destroy is needed to dispose of the percpu state which might have been allocated in percpu_counter_init based on whether CONFIG_SMP is enabled or not? Also, the call to kfree is required.
Quoted 4 lines
fs/btrfs/extent-tree.c +0
-0return ret; }
Greg Kroah-Hartman
Reviewed-byRe: [PATCH 1/2] fs: btrfs: Fix error path kobject memory leak
Quoted 26 lines · Nikolay Borisov
Quoted 23 lines · Tobin C. Harding
If a call to
kobject_init_and_add()fails we must callkobject_put()otherwise we leak memory.Calling
kobject_put()whenkobject_init_and_add()fails drops the refcount back to 0 and calls the ktype release method.Add call to
kobject_put()in the error path of call tokobject_init_and_add().Signed-off-by Tobin C. Harding <[email protected]>--- fs/btrfs/extent-tree.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)fs/btrfs/extent-tree.c +0
-1@@ -3981,8 +3981,7 @@ static int create_space_info(struct btrfs_fs_info *info, u64 flags) info->space_info_kobj,"%s" , alloc_name(space_info->flags));if (ret) { -percpu_counter_destroy (&space_info->total_bytes_pinned);
Quoted 8 lines
fs/btrfs/extent-tree.c static int create_space_info(struct btrfs_fs_info *info, u64 flags) · line 3984
Quoted 2 lines
fs/btrfs/extent-tree.c +1
-1-kfree (space_info); +kobject_put (&space_info->kobj);If you are only fixing kobject-related code then why do you delete correct code as well?
percpu_counter_Destroyis needed to dispose of the percpu state which might have been allocated inpercpu_counter_initbased on whetherCONFIG_SMPis enabled or not? Also, the call to kfree is required.
Both of those will happen in space_info_release() when the kobject is properly disposed of with this last put to the kobject reference.
PATCH 2/2 fs: btrfs: Don't leak memory when failing add fsid
In reply to Tobin C. HardingA failed call to kobject_init_and_add() must be followed by a call to kobject_put(). Currently in the error path when adding fs_devices we are missing this call. This could be fixed by calling btrfs_sysfs_remove_fsid() if btrfs_sysfs_add_fsid() returns an error or by adding a call to kobject_put() directly in btrfs_sysfs_add_fsid(). Here we choose the second option because it prevents the slightly unusual error path handling requirements of kobject from leaking out into btrfs functions.
Add a call to kobject_put() in the error path of kobject_add_and_init(). This causes the release method to be called if kobject_init_and_add() fails. open_tree() is the function that calls btrfs_sysfs_add_fsid() and the error code in this function is already written with the assumption that the release method is called during the error path of open_tree() (as seen by the call to btrfs_sysfs_remove_fsid() under the fail_fsdev_sysfs label).
--- fs/btrfs/sysfs.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
fs/btrfs/sysfs.c +6 -1
@@ -825,7 +825,12 @@ int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs, fs_devs->fsid_kobj.kset = btrfs_kset; error =kobject_init_and_add (&fs_devs->fsid_kobj, &btrfs_ktype, parent,"%pU" , fs_devs->fsid); -return error; +if (error) { +kobject_put (&fs_devs->fsid_kobj); +return error; + } + +return 0 ; }int btrfs_sysfs_add_mounted (struct btrfs_fs_info *fs_info)
Signature · 1 line
2.21.0
Re: [PATCH 2/2] fs: btrfs: Don't leak memory when failing add fsid
Quoted 9 lines · Tobin C. Harding
A failed call to
kobject_init_and_add()must be followed by a call tokobject_put(). Currently in the error path when addingfs_deviceswe are missing this call. This could be fixed by callingbtrfs_sysfs_remove_fsid()ifbtrfs_sysfs_add_fsid()returns an error or by adding a call tokobject_put()directly inbtrfs_sysfs_add_fsid(). Here we choose the second option because it prevents the slightly unusual error path handling requirements of kobject from leaking out into btrfs functions.
Quoted 7 lines
Add a call to
kobject_put()in the error path ofkobject_add_and_init(). This causes the release method to be called ifkobject_init_and_add()fails.open_tree()is the function that callsbtrfs_sysfs_add_fsid()and the error code in this function is already written with the assumption that the release method is called during the error path ofopen_tree()(as seen by the call tobtrfs_sysfs_remove_fsid()under thefail_fsdev_sysfslabel).
I'm not familiar with the internals of kobject but btrfs_sysfs_remove_fsid calls __btrfs_sysfs_remove_fsid which in turn does kobject_del followed by kobject_put so its sequence is not exactly identical with your change. Presumably kobject_del is only required if you want to dispose of successfully registered sysfs node. This implies that __btrfs_sysfs_remove_fsid is actually broken when it comes to handling failed sysfs_add_fsid?
Quoted 25 lines
Signed-off-by Tobin C. Harding <[email protected]>--- fs/btrfs/sysfs.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)fs/btrfs/sysfs.c +6
-1@@ -825,7 +825,12 @@ int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs, fs_devs->fsid_kobj.kset = btrfs_kset; error =kobject_init_and_add (&fs_devs->fsid_kobj, &btrfs_ktype, parent,"%pU" , fs_devs->fsid); -return error; +if (error) { +kobject_put (&fs_devs->fsid_kobj); +return error; + } + +return 0 ; }int btrfs_sysfs_add_mounted (struct btrfs_fs_info *fs_info)
Re: [PATCH 2/2] fs: btrfs: Don't leak memory when failing add fsid
Quoted 20 lines · Nikolay Borisov
Quoted 9 lines · Tobin C. Harding
A failed call to
kobject_init_and_add()must be followed by a call tokobject_put(). Currently in the error path when addingfs_deviceswe are missing this call. This could be fixed by callingbtrfs_sysfs_remove_fsid()ifbtrfs_sysfs_add_fsid()returns an error or by adding a call tokobject_put()directly inbtrfs_sysfs_add_fsid(). Here we choose the second option because it prevents the slightly unusual error path handling requirements of kobject from leaking out into btrfs functions.Quoted 7 lines
Add a call to
kobject_put()in the error path ofkobject_add_and_init(). This causes the release method to be called ifkobject_init_and_add()fails.open_tree()is the function that callsbtrfs_sysfs_add_fsid()and the error code in this function is already written with the assumption that the release method is called during the error path ofopen_tree()(as seen by the call tobtrfs_sysfs_remove_fsid()under thefail_fsdev_sysfslabel).
Quoted 7 lines
I'm not familiar with the internals of kobject but
btrfs_sysfs_remove_fsidcalls__btrfs_sysfs_remove_fsidwhich in turn doeskobject_delfollowed bykobject_putso its sequence is not exactly identical with your change. Presumablykobject_delis only required if you want to dispose of successfully registered sysfs node. This implies that__btrfs_sysfs_remove_fsidis actually broken when it comes to handling failedsysfs_add_fsid?
kobject_del() is not technically required in __btrfs_sysfs_remove_fsid() since if kobject_put() drops the reference count to 0 and kobject_del() has not been called then the kobject infrastructure will call kobject_del() for us (and we get a pr_debug() message). The code sequence is correct although not _exactly_ written as the kobject authors intended (I am not one of those authors, I'm just learning).
Thanks for looking at this.
Tobin
Re: [PATCH 2/2] fs: btrfs: Don't leak memory when failing add fsid
In reply to Tobin C. HardingQuoted 30 lines · Tobin C. Harding
A failed call to
kobject_init_and_add()must be followed by a call tokobject_put(). Currently in the error path when addingfs_deviceswe are missing this call. This could be fixed by callingbtrfs_sysfs_remove_fsid()ifbtrfs_sysfs_add_fsid()returns an error or by adding a call tokobject_put()directly inbtrfs_sysfs_add_fsid(). Here we choose the second option because it prevents the slightly unusual error path handling requirements of kobject from leaking out into btrfs functions.Add a call to
kobject_put()in the error path ofkobject_add_and_init(). This causes the release method to be called ifkobject_init_and_add()fails.open_tree()is the function that callsbtrfs_sysfs_add_fsid()and the error code in this function is already written with the assumption that the release method is called during the error path ofopen_tree()(as seen by the call tobtrfs_sysfs_remove_fsid()under thefail_fsdev_sysfslabel).Signed-off-by Tobin C. Harding <[email protected]>--- fs/btrfs/sysfs.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)fs/btrfs/sysfs.c +0
-0@@ -825,7 +825,12 @@ int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs, fs_devs->fsid_kobj.kset = btrfs_kset; error =kobject_init_and_add (&fs_devs->fsid_kobj, &btrfs_ktype, parent,"%pU" , fs_devs->fsid);
fs/btrfs/sysfs.c int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs, · line 836
Quoted 10 lines
fs/btrfs/sysfs.c +6
-1-return error; +if (error) { +kobject_put (&fs_devs->fsid_kobj); +return error; + } + +return 0 ; }int btrfs_sysfs_add_mounted (struct btrfs_fs_info *fs_info)
Quoted 1 line · Tobin C. Harding
Is it ok to send patches during the merge window?
Yes (depends on subsystem), the feedback for patches that are not fixes could be delayed after the merge window closes.
Quoted 10 lines
Applies on top of
Linus' mainline tag: v5.1, happy to rebase if there are conflicts.While auditing
kobject_init_and_add()calls throughout the kernel it was found that btrfs potentially has a couple of memleaks in the error path code forkobject_init_and_add().Failing calls to
kobject_init_and_add()should be followed by a call tokobject_put()sincekobject_init_and_add()always callskobject_init().
Quoted 7 lines
Of note, adding
kobject_put()causes the release method to be called ifkobject_init_and_add()fails. For patch #1 this means we don't have to manually free thespace_infoor callpercpu_counter_destroy()since these are both done by the release method. In the second patch, I believe the added call tokobject_put()fits in with thefs_deviceslifecycle assumptions ofopen_ctree()but please could you review since I am new to this code.
We use the cleanup-after-error pattern where it's up to the callee to clean up, so it's right to do it like as you did. Patches added to the queue that's for 5.2-rcX. Thanks.