Skip to contentSkip to conversation

ramfs: Fix memory leak on ramfs_fill_super error paths

PATCH In review Axel Lin 3 participants 3 messages Nov 14, 2013

lore
Share & tools
lore

Axel Lin

author
#0 lore raw

PATCH 1/1 ramfs: Fix memory leak on ramfs_fill_super error paths

The memory leak was introduced by commit 318ceed0884 "tidy up after d_make_root() conversion".

---
 fs/ramfs/inode.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
fs/ramfs/inode.c +9 -3
index 39d1465..868a41e 100644
@@ -221,7 +221,7 @@ int ramfs_fill_super(struct super_block *sb, void *data, int silent)
 
 	err = ramfs_parse_options(data, &fsi->mount_opts);
 	if (err)
-		return err;
+		goto fail;
 
 	sb->s_maxbytes		= MAX_LFS_FILESIZE;
 	sb->s_blocksize		= PAGE_CACHE_SIZE;
@@ -232,10 +232,16 @@ int ramfs_fill_super(struct super_block *sb, void *data, int silent)
 
 	inode = ramfs_get_inode(sb, NULL, S_IFDIR | fsi->mount_opts.mode, 0);
 	sb->s_root = d_make_root(inode);
-	if (!sb->s_root)
-		return -ENOMEM;
+	if (!sb->s_root) {
+		err = -ENOMEM;
+		goto fail;
+	}
 
 	return 0;
+fail:
+	kfree(fsi);
+	sb->s_fs_info = NULL;
+	return err;
 }
 
 struct dentry *ramfs_mount(struct file_system_type *fs_type,
Signature · 1 line
1.8.1.2
#1 lore raw
Quoted 26 lines · Axel Lin

The memory leak was introduced by commit 318ceed0884 "tidy up after d_make_root() conversion".

---
 fs/ramfs/inode.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
fs/ramfs/inode.c +1 -2
index 39d1465..868a41e 100644
@@ -221,7 +221,7 @@ int ramfs_fill_super(struct super_block *sb, void *data, int silent)
 
 	err = ramfs_parse_options(data, &fsi->mount_opts);
 	if (err)
-		return err;
+		goto fail;
 
 	sb->s_maxbytes		= MAX_LFS_FILESIZE;
 	sb->s_blocksize		= PAGE_CACHE_SIZE;
@@ -232,10 +232,16 @@ int ramfs_fill_super(struct super_block *sb, void *data, int silent)
 
 	inode = ramfs_get_inode(sb, NULL, S_IFDIR | fsi->mount_opts.mode, 0);
 	sb->s_root = d_make_root(inode);
-	if (!sb->s_root)

fs/ramfs/inode.c int ramfs_fill_super(struct super_block *sb, void *data, int silent) · line 243

Quoted 10 lines
fs/ramfs/inode.c +7 -1
-		return -ENOMEM;
+	if (!sb->s_root) {
+		err = -ENOMEM;
+		goto fail;
+	}
 
 	return 0;
+fail:
+	kfree(fsi);
+	sb->s_fs_info = NULL;

Why do we need this?
mount_nodev() seems just drop super block if filler fails.

Quoted 4 lines
fs/ramfs/inode.c +1 -0
+	return err;
 }
 
 struct dentry *ramfs_mount(struct file_system_type *fs_type,
Signature · 1 line
 Kirill A. Shutemov
#2 lore raw
In reply to Axel Lin
Quoted 2 lines · Axel Lin

The memory leak was introduced by commit 318ceed0884 "tidy up after d_make_root() conversion".

No memory leaks there. Note that ->kill_sb() is called in *all* cases, so that kfree() will be done by it just fine.

It's *not* ->put_super() - that would've been called only for superblocks that had passed mount. ->kill_sb() is called for *all* of them and that often simplifies cleanup on failure exits quite nicely.