Skip to contentSkip to conversation

gfs2: Fix error path kobject memory leak

PATCH In review Andreas Gruenbacher 2 participants 5 messages May 13, 2019 GFS2 file system

lore
Share & tools
lore

Andreas Gruenbacher

author maintainer
#0 lore raw

PATCH 1/1 gfs2: Fix error path kobject memory leak

From: "Tobin C. Harding" <[email protected]>

If a call to kobject_init_and_add() fails we must call kobject_put() otherwise we leak memory.

Function gfs2_sys_fs_add always calls kobject_init_and_add() which always calls kobject_init().

It is safe to leave object destruction up to the kobject release function and never free it manually.

Remove call to kfree() and always call kobject_put() in the error path.

---
 fs/gfs2/sys.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
fs/gfs2/sys.c +1 -7
index 1787d295834e..08e4996adc23 100644
@@ -650,7 +650,6 @@ int gfs2_sys_fs_add(struct gfs2_sbd *sdp)
 	char ro[20];
 	char spectator[20];
 	char *envp[] = { ro, spectator, NULL };
-	int sysfs_frees_sdp = 0;
 
 	sprintf(ro, "RDONLY=%d", sb_rdonly(sb));
 	sprintf(spectator, "SPECTATOR=%d", sdp->sd_args.ar_spectator ? 1 : 0);
@@ -661,8 +660,6 @@ int gfs2_sys_fs_add(struct gfs2_sbd *sdp)
 	if (error)
 		goto fail_reg;
 
-	sysfs_frees_sdp = 1; /* Freeing sdp is now done by sysfs calling
-				function gfs2_sbd_release. */
 	error = sysfs_create_group(&sdp->sd_kobj, &tune_group);
 	if (error)
 		goto fail_reg;
@@ -687,10 +684,7 @@ int gfs2_sys_fs_add(struct gfs2_sbd *sdp)
 fail_reg:
 	free_percpu(sdp->sd_lkstats);
 	fs_err(sdp, "error %d adding sysfs files\n", error);
-	if (sysfs_frees_sdp)
-		kobject_put(&sdp->sd_kobj);
-	else
-		kfree(sdp);
+	kobject_put(&sdp->sd_kobj);
 	sb->s_fs_info = NULL;
 	return error;
 }
Signature · 1 line
2.20.1
#1 lore raw

Andreas,
did you intend for me to take this as a patch from the email, or will
I get a pull request with this later?

Linus

author maintainer
#2 lore raw
Quoted 3 lines · Linus Torvalds

Andreas,
did you intend for me to take this as a patch from the email, or will
I get a pull request with this later?

Sorry, I should have been more explicit. Would you mind taking this patch, please? If it's more convenient or more appropriate, I'll send a pull request instead.

Thanks,
Andreas

#3 lore raw
Quoted 4 lines · Andreas Gruenbacher

Sorry, I should have been more explicit. Would you mind taking this patch, please? If it's more convenient or more appropriate, I'll send a pull request instead.

Done.

However,I'd like to point out that when I see patches from people who I normally get a pull request from, I usually ignore them.

Particularly when they are in some thread with discussion, I'll often just assume that th epatch is part of the thread, not really meant for me in particular.

In this case I happened to notice that suddenly my participation status changed, which is why I asked, but in general I might hav ejust archived the thread with the assumption that I'll be getting the patch later as a git pull.

Just so you'll be aware of this in the future, in case I don't react...

Linus

author maintainer
#4 lore raw
Quoted 15 lines · Linus Torvalds
Quoted 4 lines · Andreas Gruenbacher

Sorry, I should have been more explicit. Would you mind taking this patch, please? If it's more convenient or more appropriate, I'll send a pull request instead.

Done.

However, I'd like to point out that when I see patches from people who I normally get a pull request from, I usually ignore them.

Particularly when they are in some thread with discussion, I'll often just assume that the patch is part of the thread, not really meant for me in particular.

Quoted 6 lines

In this case I happened to notice that suddenly my participation status changed, which is why I asked, but in general I might have just archived the thread with the assumption that I'll be getting the patch later as a git pull.

Just so you'll be aware of this in the future, in case I don't react...

Thanks.

Copying Jon because this looks like useful information for other maintainers as well.

Andreas