linux-ext4.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ext4: Free resources in the error path of ext4_mb_init.
@ 2011-08-19 15:28 Tao Ma
  2011-08-19 15:28 ` [PATCH] ext4: Free resources in some error path in ext4_fill_super Tao Ma
  2011-10-06 14:22 ` [PATCH] ext4: Free resources in the error path of ext4_mb_init Ted Ts'o
  0 siblings, 2 replies; 5+ messages in thread
From: Tao Ma @ 2011-08-19 15:28 UTC (permalink / raw)
  To: linux-ext4

From: Tao Ma <boyu.mt@taobao.com>

In commit 79a77c5ac, we move ext4_mb_init_backend after the allocation
of s_locality_group to avoid memory leak in error path, but there are
still some other error paths in ext4_mb_init that need to do the same
work. So this patch adds all the error patch for ext4_mb_init. And all
the pointers are reset to NULL in case the caller may double free them.

Signed-off-by: Tao Ma <boyu.mt@taobao.com>
---
 fs/ext4/mballoc.c |   23 +++++++++++++++--------
 1 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
index 17a5a57..e7d64d8 100644
--- a/fs/ext4/mballoc.c
+++ b/fs/ext4/mballoc.c
@@ -2490,7 +2490,7 @@ int ext4_mb_init(struct super_block *sb, int needs_recovery)
 	sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
 	if (sbi->s_locality_groups == NULL) {
 		ret = -ENOMEM;
-		goto out;
+		goto out_free_groupinfo_slab;
 	}
 	for_each_possible_cpu(i) {
 		struct ext4_locality_group *lg;
@@ -2503,9 +2503,8 @@ int ext4_mb_init(struct super_block *sb, int needs_recovery)
 
 	/* init file for buddy data */
 	ret = ext4_mb_init_backend(sb);
-	if (ret != 0) {
-		goto out;
-	}
+	if (ret != 0)
+		goto out_free_locality_groups;
 
 	if (sbi->s_proc)
 		proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
@@ -2513,11 +2512,19 @@ int ext4_mb_init(struct super_block *sb, int needs_recovery)
 
 	if (sbi->s_journal)
 		sbi->s_journal->j_commit_callback = release_blocks_on_commit;
+
+	return 0;
+
+out_free_groupinfo_slab:
+	ext4_groupinfo_destroy_slabs();
+out_free_locality_groups:
+	free_percpu(sbi->s_locality_groups);
+	sbi->s_locality_groups = NULL;
 out:
-	if (ret) {
-		kfree(sbi->s_mb_offsets);
-		kfree(sbi->s_mb_maxs);
-	}
+	kfree(sbi->s_mb_offsets);
+	sbi->s_mb_offsets = NULL;
+	kfree(sbi->s_mb_maxs);
+	sbi->s_mb_maxs = NULL;
 	return ret;
 }
 
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH] ext4: Free resources in some error path in ext4_fill_super.
  2011-08-19 15:28 [PATCH] ext4: Free resources in the error path of ext4_mb_init Tao Ma
@ 2011-08-19 15:28 ` Tao Ma
  2011-10-06 15:58   ` Ted Ts'o
  2011-10-06 14:22 ` [PATCH] ext4: Free resources in the error path of ext4_mb_init Ted Ts'o
  1 sibling, 1 reply; 5+ messages in thread
From: Tao Ma @ 2011-08-19 15:28 UTC (permalink / raw)
  To: linux-ext4

From: Tao Ma <boyu.mt@taobao.com>

Some of the error path in ext4_fill_super don't release the
resouces properly. So this patch just try to release them
in the right way.

Signed-off-by: Tao Ma <boyu.mt@taobao.com>
---
 fs/ext4/super.c |   19 +++++++++++--------
 1 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 4687fea..552b465 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -3711,22 +3711,19 @@ no_journal:
 	if (err) {
 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
 			 err);
-		goto failed_mount4;
+		goto failed_mount5;
 	}
 
 	err = ext4_register_li_request(sb, first_not_zeroed);
 	if (err)
-		goto failed_mount4;
+		goto failed_mount6;
 
 	sbi->s_kobj.kset = ext4_kset;
 	init_completion(&sbi->s_kobj_unregister);
 	err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
 				   "%s", sb->s_id);
-	if (err) {
-		ext4_mb_release(sb);
-		ext4_ext_release(sb);
-		goto failed_mount4;
-	};
+	if (err)
+		goto failed_mount7;
 
 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
 	ext4_orphan_cleanup(sb, es);
@@ -3760,13 +3757,19 @@ cantfind_ext4:
 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
 	goto failed_mount;
 
+failed_mount7:
+	ext4_unregister_li_request(sb);
+failed_mount6:
+	ext4_ext_release(sb);
+failed_mount5:
+	ext4_mb_release(sb);
+	ext4_release_system_zone(sb);
 failed_mount4:
 	iput(root);
 	sb->s_root = NULL;
 	ext4_msg(sb, KERN_ERR, "mount failed");
 	destroy_workqueue(EXT4_SB(sb)->dio_unwritten_wq);
 failed_mount_wq:
-	ext4_release_system_zone(sb);
 	if (sbi->s_journal) {
 		jbd2_journal_destroy(sbi->s_journal);
 		sbi->s_journal = NULL;
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] ext4: Free resources in the error path of ext4_mb_init.
  2011-08-19 15:28 [PATCH] ext4: Free resources in the error path of ext4_mb_init Tao Ma
  2011-08-19 15:28 ` [PATCH] ext4: Free resources in some error path in ext4_fill_super Tao Ma
@ 2011-10-06 14:22 ` Ted Ts'o
  2011-10-07 15:07   ` [PATCH v2] " Tao Ma
  1 sibling, 1 reply; 5+ messages in thread
From: Ted Ts'o @ 2011-10-06 14:22 UTC (permalink / raw)
  To: Tao Ma; +Cc: linux-ext4

On Fri, Aug 19, 2011 at 11:28:08PM +0800, Tao Ma wrote:
> From: Tao Ma <boyu.mt@taobao.com>
> 
> In commit 79a77c5ac, we move ext4_mb_init_backend after the allocation
> of s_locality_group to avoid memory leak in error path, but there are
> still some other error paths in ext4_mb_init that need to do the same
> work. So this patch adds all the error patch for ext4_mb_init. And all
> the pointers are reset to NULL in case the caller may double free them.
> 
> Signed-off-by: Tao Ma <boyu.mt@taobao.com>

but I had to reorder the cleanup code for
"out_free_groupinfo_slab" and "out_free_locality_groups" in the
following patch hunk....


> @@ -2513,11 +2512,19 @@ int ext4_mb_init(struct super_block *sb, int needs_recovery)
>  
>  	if (sbi->s_journal)
>  		sbi->s_journal->j_commit_callback = release_blocks_on_commit;
> +
> +	return 0;
> +
> +out_free_groupinfo_slab:
> +	ext4_groupinfo_destroy_slabs();
> +out_free_locality_groups:
> +	free_percpu(sbi->s_locality_groups);
> +	sbi->s_locality_groups = NULL;

Since we first allocate the groupinfo slabs, and then the locality
groups, so the cleanup paths need to do things in the opposite
order.

						- Ted

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] ext4: Free resources in some error path in ext4_fill_super.
  2011-08-19 15:28 ` [PATCH] ext4: Free resources in some error path in ext4_fill_super Tao Ma
@ 2011-10-06 15:58   ` Ted Ts'o
  0 siblings, 0 replies; 5+ messages in thread
From: Ted Ts'o @ 2011-10-06 15:58 UTC (permalink / raw)
  To: Tao Ma; +Cc: linux-ext4

On Fri, Aug 19, 2011 at 11:28:09PM +0800, Tao Ma wrote:
> From: Tao Ma <boyu.mt@taobao.com>
> 
> Some of the error path in ext4_fill_super don't release the
> resouces properly. So this patch just try to release them
> in the right way.
> 
> Signed-off-by: Tao Ma <boyu.mt@taobao.com>

Thanks, applied.

						- Ted

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2] ext4: Free resources in the error path of ext4_mb_init.
  2011-10-06 14:22 ` [PATCH] ext4: Free resources in the error path of ext4_mb_init Ted Ts'o
@ 2011-10-07 15:07   ` Tao Ma
  0 siblings, 0 replies; 5+ messages in thread
From: Tao Ma @ 2011-10-07 15:07 UTC (permalink / raw)
  To: linux-ext4; +Cc: Theodore Ts'o

From: Tao Ma <boyu.mt@taobao.com>

In commit 79a77c5ac, we move ext4_mb_init_backend after the allocation
of s_locality_group to avoid memory leak in error path, but there are
still some other error paths in ext4_mb_init that need to do the same
work. So this patch adds all the error patch for ext4_mb_init. And all
the pointers are reset to NULL in case the caller may double free them.

Cc: "Theodore Ts'o" <tytso@mit.edu>
Signed-off-by: Tao Ma <boyu.mt@taobao.com>
---
 fs/ext4/mballoc.c |   23 +++++++++++++++--------
 1 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
index 17a5a57..1e194c5 100644
--- a/fs/ext4/mballoc.c
+++ b/fs/ext4/mballoc.c
@@ -2490,7 +2490,7 @@ int ext4_mb_init(struct super_block *sb, int needs_recovery)
 	sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
 	if (sbi->s_locality_groups == NULL) {
 		ret = -ENOMEM;
-		goto out;
+		goto out_free_groupinfo_slab;
 	}
 	for_each_possible_cpu(i) {
 		struct ext4_locality_group *lg;
@@ -2503,9 +2503,8 @@ int ext4_mb_init(struct super_block *sb, int needs_recovery)
 
 	/* init file for buddy data */
 	ret = ext4_mb_init_backend(sb);
-	if (ret != 0) {
-		goto out;
-	}
+	if (ret != 0)
+		goto out_free_locality_groups;
 
 	if (sbi->s_proc)
 		proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
@@ -2513,11 +2512,19 @@ int ext4_mb_init(struct super_block *sb, int needs_recovery)
 
 	if (sbi->s_journal)
 		sbi->s_journal->j_commit_callback = release_blocks_on_commit;
+
+	return 0;
+
+out_free_locality_groups:
+	free_percpu(sbi->s_locality_groups);
+	sbi->s_locality_groups = NULL;
+out_free_groupinfo_slab:
+	ext4_groupinfo_destroy_slabs();
 out:
-	if (ret) {
-		kfree(sbi->s_mb_offsets);
-		kfree(sbi->s_mb_maxs);
-	}
+	kfree(sbi->s_mb_offsets);
+	sbi->s_mb_offsets = NULL;
+	kfree(sbi->s_mb_maxs);
+	sbi->s_mb_maxs = NULL;
 	return ret;
 }
 
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2011-10-07 15:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-08-19 15:28 [PATCH] ext4: Free resources in the error path of ext4_mb_init Tao Ma
2011-08-19 15:28 ` [PATCH] ext4: Free resources in some error path in ext4_fill_super Tao Ma
2011-10-06 15:58   ` Ted Ts'o
2011-10-06 14:22 ` [PATCH] ext4: Free resources in the error path of ext4_mb_init Ted Ts'o
2011-10-07 15:07   ` [PATCH v2] " Tao Ma

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).