Skip to contentSkip to conversation

shmem: put inode if alloc_file failed

PATCH In review Bob Liu 3 participants 7 messages Mar 8, 2011 – Mar 11, 2011

Subsystems (2)
lore
Share & tools
lore

Bob Liu

author
#0 lore raw

PATCH 1/1 shmem: put inode if alloc_file failed

Currently if alloc_file failed, inode willn't be put which may cause small memory leak, this patch fix it.

---
 mm/shmem.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
mm/shmem.c +4 -2
index 7c9cdc6..1e2bea7 100644
@@ -2756,17 +2756,19 @@ struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags
 #ifndef CONFIG_MMU
 	error = ramfs_nommu_expand_for_mapping(inode, size);
 	if (error)
-		goto put_dentry;
+		goto put_inode;
 #endif
 
 	error = -ENFILE;
 	file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
 		  &shmem_file_operations);
 	if (!file)
-		goto put_dentry;
+		goto put_inode;
 
 	return file;
 
+put_inode:
+	iput(inode);
 put_dentry:
 	path_put(&path);
 put_memory:
Signature · 8 lines
1.6.3.3

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
author
#1 lore raw

PATCH 1/1 shmem: using goto to replace several return

Code clean, use goto to replace several return.

---
 mm/shmem.c |   32 +++++++++++++++-----------------
 1 files changed, 15 insertions(+), 17 deletions(-)
mm/shmem.c +15 -17
index 7c9cdc6..99f5915 100644
@@ -1847,17 +1847,13 @@ shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
 						     &dentry->d_name, NULL,
 						     NULL, NULL);
 		if (error) {
-			if (error != -EOPNOTSUPP) {
-				iput(inode);
-				return error;
-			}
+			if (error != -EOPNOTSUPP)
+				goto failed_iput;
 		}
 #ifdef CONFIG_TMPFS_POSIX_ACL
 		error = generic_acl_init(inode, dir);
-		if (error) {
-			iput(inode);
-			return error;
-		}
+		if (error)
+			goto failed_iput;
 #else
 		error = 0;
 #endif
@@ -1866,6 +1862,9 @@ shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
 		d_instantiate(dentry, inode);
 		dget(dentry); /* Extra count - pin the dentry in core */
 	}
+
+failed_iput:
+	iput(inode);
 	return error;
 }
 
@@ -1987,10 +1986,8 @@ static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *s
 	error = security_inode_init_security(inode, dir, &dentry->d_name, NULL,
 					     NULL, NULL);
 	if (error) {
-		if (error != -EOPNOTSUPP) {
-			iput(inode);
-			return error;
-		}
+		if (error != -EOPNOTSUPP)
+			goto failed_iput;
 		error = 0;
 	}
 
@@ -2002,10 +1999,8 @@ static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *s
 		inode->i_op = &shmem_symlink_inline_operations;
 	} else {
 		error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
-		if (error) {
-			iput(inode);
-			return error;
-		}
+		if (error)
+			goto failed_iput;
 		inode->i_mapping->a_ops = &shmem_aops;
 		inode->i_op = &shmem_symlink_inode_operations;
 		kaddr = kmap_atomic(page, KM_USER0);
@@ -2019,7 +2014,10 @@ static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *s
 	dir->i_ctime = dir->i_mtime = CURRENT_TIME;
 	d_instantiate(dentry, inode);
 	dget(dentry);
-	return 0;
+
+failed_iput:
+	iput(inode);
+	return error;
 }
 
 static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
Signature · 8 lines
1.6.3.3

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#2 lore raw

Re: [PATCH] shmem: using goto to replace several return

Quoted 25 lines · On Tue, 8 Mar 2011 17:15:00 +0800 Bob Liu <[email protected]> wrote:
mm/shmem.c +4 -8
@@ -1847,17 +1847,13 @@ shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
 						     &dentry->d_name, NULL,
 						     NULL, NULL);
 		if (error) {
-			if (error != -EOPNOTSUPP) {
-				iput(inode);
-				return error;
-			}
+			if (error != -EOPNOTSUPP)
+				goto failed_iput;
 		}
 #ifdef CONFIG_TMPFS_POSIX_ACL
 		error = generic_acl_init(inode, dir);
-		if (error) {
-			iput(inode);
-			return error;
-		}
+		if (error)
+			goto failed_iput;
 #else
 		error = 0;
 #endif

mm/shmem.c shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) · line 1869

Quoted 8 lines
mm/shmem.c +3 -0
@@ -1866,6 +1862,9 @@ shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
 		d_instantiate(dentry, inode);
 		dget(dentry); /* Extra count - pin the dentry in core */
 	}
+
+failed_iput:
+	iput(inode);
 	return error;
 }

This adds a big bug: we newly do iput() on the non-error path.

Please, we need much more care and testing than this.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#3 lore raw
In reply to Bob Liu
Quoted 24 lines · On Tue, 8 Mar 2011 17:14:59 +0800 Bob Liu <[email protected]> wrote:

Currently if alloc_file failed, inode willn't be put which may cause small memory leak, this patch fix it.

---
 mm/shmem.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
mm/shmem.c +1 -1
index 7c9cdc6..1e2bea7 100644
@@ -2756,17 +2756,19 @@ struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags
 #ifndef CONFIG_MMU
 	error = ramfs_nommu_expand_for_mapping(inode, size);
 	if (error)
-		goto put_dentry;
+		goto put_inode;
 #endif
 
 	error = -ENFILE;
 	file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
 		  &shmem_file_operations);
 	if (!file)

mm/shmem.c struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags · line 2774

Quoted 10 lines
mm/shmem.c +3 -1
-		goto put_dentry;
+		goto put_inode;
 
 	return file;
 
+put_inode:
+	iput(inode);
 put_dentry:
 	path_put(&path);
 put_memory:

Is this correct? We've linked the inode to the dentry with d_instantiate(), so the d_put() will do the iput() on the inode.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#4 lore raw
Quoted 8 lines · Andrew Morton

mm/shmem.c struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags · line 2774

Quoted 5 lines
mm/shmem.c +2 -0
+put_inode:
+	iput(inode);
 put_dentry:
 	path_put(&path);
 put_memory:

Is this correct? We've linked the inode to the dentry with d_instantiate(), so the d_put() will do the iput() on the inode.

ITYM path_put() and yes, it will. There's no leak.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author
#5 lore raw
Quoted 8 lines · Al Viro
Quoted 6 lines · Andrew Morton
Quoted 5 lines

+put_inode:
+   iput(inode);
 put_dentry:
    path_put(&path);
 put_memory:

Quoted 6 lines
Quoted 2 lines

Is this correct?  We've linked the inode to the dentry with d_instantiate(), so the d_put() will do the iput() on the inode.

ITYM path_put() and yes, it will.  There's no leak.

Hi,

Yes, I also think path_put() should, But it seems iput() can't free that memory during NO-MMU arch.

Would you please take a look at thread: [BUG?] shmem: memory leak on NO-MMU arch ?

http://www.spinics.net/lists/linux-mm/msg15668.html

Thanks

Signature · 9 lines
Regards,
--Bob

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
author
#6 lore raw
In reply to Al Viro
Quoted 8 lines · Al Viro
Quoted 6 lines · Andrew Morton
Quoted 5 lines

+put_inode:
+   iput(inode);
 put_dentry:
    path_put(&path);
 put_memory:

Quoted 6 lines
Quoted 2 lines

Is this correct?  We've linked the inode to the dentry with d_instantiate(), so the d_put() will do the iput() on the inode.

ITYM path_put() and yes, it will.  There's no leak.

In my opinion, on no-mmu arch iput() will call generate_delete_inode()

Quoted 1 line

truncate_page_range() which just delete the page from pagecache.

But pages allocated by ramfs_nommu_expand_for_mapping() have page->_count = 2 (one by alloc_pages + one by pagecache). The page->_count is still 1 after iput(), so the memory can't be freed.

Is there something I am wrong?

Thanks.

Signature · 9 lines
Regards,
--Bob

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>