Skip to contentSkip to conversation

misc kernel leak patches

PATCH Applied Mark Tinguely 4 participants 8 messages Oct 2, 2013 – Dec 5, 2013 XFS filesystem

v1 of 3 Applied Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover misc kernel leak patches

Here are a couple patches to free leaked memory.

First patch is v2 of a Coverity found leak in xfs_dir2_node_removename().

Second patch arose from an eariler leak found by Coverity. In looking at that patch, Eric noted that it appeared that the transaction's item list is also leaked if recovery fails. I tested this patch by forcing errors into the recovery code to verify that the items are now being released.

I will send patches for xfsprogs equivalent.

--Mark.

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#4 lore raw

PATCH 2/2 xfs free the list of recovery items on error.

Recovery builds a list of items on the transaction's r_itemq head. Normally these items are committed and freed. But in the event of a recovery error, these allocations are leaked.

If the error occurs during item reordering, then reconstruct the r_itemq list before deleting the list to avoid leaking the entries that were on one of the temporary lists.

---
 fs/xfs/xfs_log_recover.c |   17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
fs/xfs/xfs_log_recover.c +14 -3
Index: b/fs/xfs/xfs_log_recover.c
===================================================================
@@ -1659,6 +1659,7 @@ xlog_recover_reorder_trans(
 	int			pass)
 {
 	xlog_recover_item_t	*item, *n;
+	int			error = 0;
 	LIST_HEAD(sort_list);
 	LIST_HEAD(cancel_list);
 	LIST_HEAD(buffer_list);
@@ -1700,9 +1701,17 @@ xlog_recover_reorder_trans(
 				"%s: unrecognized type of log operation",
 				__func__);
 			ASSERT(0);
-			return XFS_ERROR(EIO);
+			/*
+			 * return the remaining items back to the transaction
+			 * item list so they can be freed in caller.
+			 */
+			if (!list_empty(&sort_list))
+				list_splice_init(&sort_list, &trans->r_itemq);
+			error = XFS_ERROR(EIO);
+			goto out;
 		}
 	}
+out:
 	ASSERT(list_empty(&sort_list));
 	if (!list_empty(&buffer_list))
 		list_splice(&buffer_list, &trans->r_itemq);
@@ -1712,7 +1721,7 @@ xlog_recover_reorder_trans(
 		list_splice_tail(&inode_buffer_list, &trans->r_itemq);
 	if (!list_empty(&cancel_list))
 		list_splice_tail(&cancel_list, &trans->r_itemq);
-	return 0;
+	return error;
 }
 
 /*
@@ -3743,8 +3752,10 @@ xlog_recover_process_data(
 				error = XFS_ERROR(EIO);
 				break;
 			}
-			if (error)
+			if (error) {
+				xlog_recover_free_trans(trans);
 				return error;
+			}
 		}
 		dp += be32_to_cpu(ohead->oh_len);
 		num_logops--;

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#1 lore raw

PATCH 1/2 xfs: fix memory leak in xfs_dir2_node_removename

In reply to Mark Tinguely

Fix the leak of kernel memory in xfs_dir2_node_removename() when xfs_dir2_leafn_remove() returns an error code.

Found by Coverity in userspace same patch applies there also.

---
v2 corrected bad return code as pointed out by Roger Willcocks.

 fs/xfs/xfs_dir2_node.c |   14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
fs/xfs/xfs_dir2_node.c +8 -6
Index: b/fs/xfs/xfs_dir2_node.c
===================================================================
@@ -2105,12 +2105,12 @@ xfs_dir2_node_lookup(
  */
 int						/* error */
 xfs_dir2_node_removename(
-	xfs_da_args_t		*args)		/* operation arguments */
+	struct xfs_da_args	*args)		/* operation arguments */
 {
-	xfs_da_state_blk_t	*blk;		/* leaf block */
+	struct xfs_da_state_blk	*blk;		/* leaf block */
 	int			error;		/* error return value */
 	int			rval;		/* operation return value */
-	xfs_da_state_t		*state;		/* btree cursor */
+	struct xfs_da_state	*state;		/* btree cursor */
 
 	trace_xfs_dir2_node_removename(args);
 
@@ -2132,9 +2132,10 @@ xfs_dir2_node_removename(
 	 * Didn't find it, upper layer screwed up.
 	 */
 	if (rval != EEXIST) {
-		xfs_da_state_free(state);
-		return rval;
+		error = rval;
+		goto done;
 	}
+
 	blk = &state->path.blk[state->path.active - 1];
 	ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
 	ASSERT(state->extravalid);
@@ -2145,7 +2146,7 @@ xfs_dir2_node_removename(
 	error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
 		&state->extrablk, &rval);
 	if (error)
-		return error;
+		goto done;
 	/*
 	 * Fix the hash values up the btree.
 	 */
@@ -2160,6 +2161,7 @@ xfs_dir2_node_removename(
 	 */
 	if (!error)
 		error = xfs_dir2_node_to_leaf(state);
+done:
 	xfs_da_state_free(state);
 	return error;
 }

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#2 lore raw

Re: [PATCH 1/2] xfs: fix memory leak in xfs_dir2_node_removename

Quoted 33 lines · Mark Tinguely

Fix the leak of kernel memory in xfs_dir2_node_removename() when xfs_dir2_leafn_remove() returns an error code.

Found by Coverity in userspace same patch applies there also.

---
v2 corrected bad return code as pointed out by Roger Willcocks.

 fs/xfs/xfs_dir2_node.c |   14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

Index: b/fs/xfs/xfs_dir2_node.c
===================================================================

fs/xfs/xfs_dir2_node.c +3 -3
@@ -2105,12 +2105,12 @@ xfs_dir2_node_lookup(
  */
 int						/* error */
 xfs_dir2_node_removename(
-	xfs_da_args_t		*args)		/* operation arguments */
+	struct xfs_da_args	*args)		/* operation arguments */
 {
-	xfs_da_state_blk_t	*blk;		/* leaf block */
+	struct xfs_da_state_blk	*blk;		/* leaf block */
 	int			error;		/* error return value */
 	int			rval;		/* operation return value */
-	xfs_da_state_t		*state;		/* btree cursor */
+	struct xfs_da_state	*state;		/* btree cursor */
 
 	trace_xfs_dir2_node_removename(args);
 

fs/xfs/xfs_dir2_node.c xfs_dir2_node_removename( · line 2136

Quoted 7 lines
fs/xfs/xfs_dir2_node.c +2 -2
@@ -2132,9 +2132,10 @@ xfs_dir2_node_removename(
 	 * Didn't find it, upper layer screwed up.
 	 */
 	if (rval != EEXIST) {
-		xfs_da_state_free(state);
-		return rval;
+		error = rval;
+		goto done;

Can you make this something like "out_free"? We tend to name jump labels according to the action that needs to be taken, like "out_unlock", "error_trans_cancel", etc...

Also, this code is now "funky" in how it handles rval. it will do this:

        /*
         * Look up the entry we're deleting, set up the cursor.
         */
        error = xfs_da3_node_lookup_int(state, &rval);
        if (error)
                rval = error;
        /*
         * Didn't find it, upper layer screwed up.
         */
        if (rval != EEXIST) {
		error = rval;
                goto out_free;
        }

That's kind funky with the reassignment of rval if there's an error. Better would be:

        /* Look up the entry we're deleting, set up the cursor. */
        error = xfs_da3_node_lookup_int(state, &rval);
        if (error)
                goto out_free;

        /* Didn't find it, upper layer screwed up. */
	if (rval != EEXIST) {
		error = rval;
		goto out_free;
	}

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#7 lore raw
In reply to Mark Tinguely
Quoted 10 lines · Mark Tinguely

Here are a couple patches to free leaked memory.

First patch is v2 of a Coverity found leak in xfs_dir2_node_removename().

Second patch arose from an eariler leak found by Coverity. In looking at that patch, Eric noted that it appeared that the transaction's item list is also leaked if recovery fails. I tested this patch by forcing errors into the recovery code to verify that the items are now being released.

I will send patches for xfsprogs equivalent.

Good plan w.r.t. kernel first, progs 2nd, I think. Makes it less likely to accidentally send a free(); or similar upstream. :)

-Eric

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#3 lore raw

Re: [PATCH 1/2] xfs: fix memory leak in xfs_dir2_node_removename

In reply to Dave Chinner
Quoted 77 lines · Dave Chinner
Quoted 33 lines · Mark Tinguely

Fix the leak of kernel memory in xfs_dir2_node_removename() when xfs_dir2_leafn_remove() returns an error code.

Found by Coverity in userspace same patch applies there also.

---
v2 corrected bad return code as pointed out by Roger Willcocks.

  fs/xfs/xfs_dir2_node.c |   14 ++++++++------
  1 file changed, 8 insertions(+), 6 deletions(-)

Index: b/fs/xfs/xfs_dir2_node.c
===================================================================

fs/xfs/xfs_dir2_node.c +3 -3
@@ -2105,12 +2105,12 @@ xfs_dir2_node_lookup(
   */
  int						/* error */
  xfs_dir2_node_removename(
-	xfs_da_args_t		*args)		/* operation arguments */
+	struct xfs_da_args	*args)		/* operation arguments */
  {
-	xfs_da_state_blk_t	*blk;		/* leaf block */
+	struct xfs_da_state_blk	*blk;		/* leaf block */
  	int			error;		/* error return value */
  	int			rval;		/* operation return value */
-	xfs_da_state_t		*state;		/* btree cursor */
+	struct xfs_da_state	*state;		/* btree cursor */
 
  	trace_xfs_dir2_node_removename(args);
 

fs/xfs/xfs_dir2_node.c xfs_dir2_node_removename( · line 2136

Quoted 7 lines
fs/xfs/xfs_dir2_node.c +2 -2
@@ -2132,9 +2132,10 @@ xfs_dir2_node_removename(
  	 * Didn't find it, upper layer screwed up.
  	 */
  	if (rval != EEXIST) {
-		xfs_da_state_free(state);
-		return rval;
+		error = rval;
+		goto done;

Can you make this something like "out_free"? We tend to name jump labels according to the action that needs to be taken, like "out_unlock", "error_trans_cancel", etc...

Also, this code is now "funky" in how it handles rval. it will do this:

         /*
          * Look up the entry we're deleting, set up the cursor.
          */
         error = xfs_da3_node_lookup_int(state,&rval);
         if (error)
                 rval = error;
         /*
          * Didn't find it, upper layer screwed up.
          */
         if (rval != EEXIST) {
		error = rval;
                 goto out_free;
         }

That's kind funky with the reassignment of rval if there's an error. Better would be:

         /* Look up the entry we're deleting, set up the cursor. */
         error = xfs_da3_node_lookup_int(state,&rval);
         if (error)
                 goto out_free;

         /* Didn't find it, upper layer screwed up. */
	if (rval != EEXIST) {
		error = rval;
		goto out_free;
	}
Quoted 3 lines

Cheers,

Dave.

nod.

thank

--Mark.

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] xfs free the list of recovery items on error.

In reply to Mark Tinguely
Quoted 10 lines · Mark Tinguely

Recovery builds a list of items on the transaction's r_itemq head. Normally these items are committed and freed. But in the event of a recovery error, these allocations are leaked.

If the error occurs during item reordering, then reconstruct the r_itemq list before deleting the list to avoid leaking the entries that were on one of the temporary lists.

Looks good to me.

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

Applied
#6 lore raw

Re: [PATCH 2/2] xfs free the list of recovery items on error.

Quoted 6 lines · Ben Myers
Quoted 5 lines · Mark Tinguely

Recovery builds a list of items on the transaction's r_itemq head. Normally these items are committed and freed. But in the event of a recovery error, these allocations are leaked.

Quoted 8 lines
Quoted 5 lines

If the error occurs during item reordering, then reconstruct the r_itemq list before deleting the list to avoid leaking the entries that were on one of the temporary lists.

Looks good to me.

Applied.

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs