Skip to contentSkip to conversation

XFS: memory leak in xfs_inactive() - is xfs_trans_free() enough or do we need xfs_trans_cancel() ?

RFC PATCH In review Jesper Juhl 2 participants 4 messages May 16, 2007 – May 24, 2007 XFS filesystem

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 XFS: memory leak in xfs_inactive() - is xfs_trans_free() enough or do we need xfs_trans_cancel() ?

Hi,

The Coverity checker found a memory leak in xfs_inactive().

The offending code is this bit :

1671 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);

At conditional (1): "truncate != 0" taking true path

1672 if (truncate) {
1673 /*
1674 * Do the xfs_itruncate_start() call before
1675 * reserving any log space because itruncate_start
1676 * will call into the buffer cache and we can't
1677 * do that within a transaction.
1678 */
1679 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1680
1681 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);

At conditional (2): "error != 0" taking true path

1682 if (error) {
1683 xfs_iunlock(ip, XFS_IOLOCK_EXCL);

Event leaked_storage: Returned without freeing storage "tp" Also see events: [alloc_fn][var_assign]

1684 return VN_INACTIVE_CACHE;
1685 }

So, the code allocates a transaction, but in the case where 'truncate' is !=0 and xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0); happens to return an error, we'll just return from the function without dealing with the memory allocated byxfs_trans_alloc() and assigned to 'tp', thus it'll be orphaned/leaked - not good.

What I'm wondering is this; is it enough, at this point, to call xfs_trans_free(tp); (it would seem to me that would be OK, but I'm not intimite with this code) or do we need a full xfs_trans_cancel(tp, 0); ???

In case I'm right and xfs_trans_free(tp); is all we need, then please consider the patch below. Otherwise please NACK the patch and I'll cook up another one :-)

Fix memory leak on error in xfs_inactive().

---

 fs/xfs/xfs_vnodeops.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
fs/xfs/xfs_vnodeops.c +1 -0
index de17aed..e0d3d51 100644
@@ -1681,6 +1681,7 @@ xfs_inactive(
 		error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
 		if (error) {
 			xfs_iunlock(ip, XFS_IOLOCK_EXCL);
+			xfs_trans_free(tp);
 			return VN_INACTIVE_CACHE;
 		}
 
Signature · 3 lines
Jesper Juhl <[email protected]>
Don't top-post  http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please      http://www.expita.com/nomime.html
#1 lore raw
Quoted 3 lines · Jesper Juhl

Hi,

The Coverity checker found a memory leak in xfs_inactive().

....

Quoted 5 lines

So, the code allocates a transaction, but in the case where 'truncate' is !=0 and xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0); happens to return an error, we'll just return from the function without dealing with the memory allocated byxfs_trans_alloc() and assigned to 'tp', thus it'll be orphaned/leaked - not good.

Yeah, introduced by:

http://git2.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commit;h=d3cf209476b72c83907a412b6708c5e498410aa7

Thanks for reporting the problem, Jesper.

Quoted 3 lines

What I'm wondering is this; is it enough, at this point, to call xfs_trans_free(tp); (it would seem to me that would be OK, but I'm not intimite with this code) or do we need a full xfs_trans_cancel(tp, 0); ???

xfs_trans_free() is not supposed to be called by anything but the transaction code (it's static). So a xfs_trans_cancel() would need to be issued.

Quoted 3 lines

In case I'm right and xfs_trans_free(tp); is all we need, then please consider the patch below. Otherwise please NACK the patch and I'll cook up another one :-)

NACK ;)

xfs_trans_cancel() is needed. Patch below.

Cheers,

Dave.

Signature · 20 lines
Dave Chinner
Principal Engineer
SGI Australian Software Group

---
 fs/xfs/xfs_vnodeops.c |    1 +
 1 file changed, 1 insertion(+)

Index: 2.6.x-xfs-new/fs/xfs/xfs_vnodeops.c
===================================================================
--- 2.6.x-xfs-new.orig/fs/xfs/xfs_vnodeops.c	2007-05-11 16:04:03.000000000 +1000
+++ 2.6.x-xfs-new/fs/xfs/xfs_vnodeops.c	2007-05-17 12:37:25.671399078 +1000
@@ -1710,6 +1710,7 @@ xfs_inactive(
 
 		error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
 		if (error) {
+			xfs_trans_cancel(tp, 0);
 			xfs_iunlock(ip, XFS_IOLOCK_EXCL);
 			return VN_INACTIVE_CACHE;
 		}
#2 lore raw
Quoted 11 lines · On Thursday 17 May 2007 04:40:24 David Chinner wrote:
Quoted 3 lines · Jesper Juhl

Hi,

The Coverity checker found a memory leak in xfs_inactive().

....

Quoted 5 lines

So, the code allocates a transaction, but in the case where 'truncate' is !=0 and xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0); happens to return an error, we'll just return from the function without dealing with the memory allocated byxfs_trans_alloc() and assigned to 'tp', thus it'll be orphaned/leaked - not good.

Quoted 6 lines

Yeah, introduced by:

http://git2.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commit;h=d3cf209476b72c83907a412b6708c5e498410aa7

Thanks for reporting the problem, Jesper.

You are welcome.

That commit introduces the same problem in xfs_inactive_free_eofblocks(). Patch to fix it below.

Quoted 7 lines
Quoted 3 lines

What I'm wondering is this; is it enough, at this point, to call xfs_trans_free(tp); (it would seem to me that would be OK, but I'm not intimite with this code) or do we need a full xfs_trans_cancel(tp, 0); ???

xfs_trans_free() is not supposed to be called by anything but the transaction code (it's static). So a xfs_trans_cancel() would need to be issued.

Makes sense. Thanks. I completely missed the static nature :-/

Fix XFS memory leak; allocated transaction not freed in xfs_inactive_free_eofblocks() in failure case.

---

 fs/xfs/xfs_vnodeops.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
fs/xfs/xfs_vnodeops.c +1 -0
index de17aed..32519cf 100644
@@ -1260,6 +1260,7 @@ xfs_inactive_free_eofblocks(
 		error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
 				    ip->i_size);
 		if (error) {
+			xfs_trans_cancel(tp, 0);
 			xfs_iunlock(ip, XFS_IOLOCK_EXCL);
 			return error;
 		}
#3 lore raw

Any chance the patches below that fix two mem leaks in XFS will make it in in time for 2.6.22? I believe they should...

Quoted 55 lines · Jesper Juhl
Quoted 11 lines · On Thursday 17 May 2007 04:40:24 David Chinner wrote:
Quoted 3 lines · Jesper Juhl

Hi,

The Coverity checker found a memory leak in xfs_inactive().

....

Quoted 5 lines

So, the code allocates a transaction, but in the case where 'truncate' is !=0 and xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0); happens to return an error, we'll just return from the function without dealing with the memory allocated byxfs_trans_alloc() and assigned to 'tp', thus it'll be orphaned/leaked - not good.

Quoted 6 lines

Yeah, introduced by:

http://git2.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commit;h=d3cf209476b72c83907a412b6708c5e498410aa7

Thanks for reporting the problem, Jesper.

You are welcome.

That commit introduces the same problem in xfs_inactive_free_eofblocks(). Patch to fix it below.

Quoted 7 lines
Quoted 3 lines

What I'm wondering is this; is it enough, at this point, to call xfs_trans_free(tp); (it would seem to me that would be OK, but I'm not intimite with this code) or do we need a full xfs_trans_cancel(tp, 0); ???

xfs_trans_free() is not supposed to be called by anything but the transaction code (it's static). So a xfs_trans_cancel() would need to be issued.

Makes sense. Thanks. I completely missed the static nature :-/

Fix XFS memory leak; allocated transaction not freed in xfs_inactive_free_eofblocks() in failure case.

---
 fs/xfs/xfs_vnodeops.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
fs/xfs/xfs_vnodeops.c +1 -0
index de17aed..32519cf 100644
@@ -1260,6 +1260,7 @@ xfs_inactive_free_eofblocks(
                error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
                                    ip->i_size);
                if (error) {
+                       xfs_trans_cancel(tp, 0);
                        xfs_iunlock(ip, XFS_IOLOCK_EXCL);
                        return error;
                }
Signature · 3 lines
Jesper Juhl <[email protected]>
Don't top-post  http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please      http://www.expita.com/nomime.html