linux-ext4.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ext4: implement error handling of ext4_mb_new_preallocation()
@ 2013-05-05 20:51 Alexey Khoroshilov
  2013-05-06 19:24 ` Darrick J. Wong
  2013-06-17 13:17 ` Theodore Ts'o
  0 siblings, 2 replies; 3+ messages in thread
From: Alexey Khoroshilov @ 2013-05-05 20:51 UTC (permalink / raw)
  To: Theodore Ts'o
  Cc: Alexey Khoroshilov, Andreas Dilger, linux-ext4, linux-kernel,
	spruce-project

If memory allocation in ext4_mb_new_group_pa() is failed,
it returns error code, ext4_mb_new_preallocation() propages it,
but ext4_mb_new_blocks() ignores it.

An observed result was:
- allocation fail means ext4_mb_new_group_pa() does not update ext4_allocation_context;
- ext4_mb_new_blocks() sets ext4_allocation_request->len (ar->len = ac->ac_b_ex.fe_len;)
  to number of blocks preallocated (512) instead of number of blocks requested (1);
- that activates update cycle in ext4_splice_branch():
    for (i = 1; i < blks; i++) <-- blks is 512 instead of 1 here
      *(where->p + i) = cpu_to_le32(current_block++);
- it iterates 511 times and corrupts a chunk of memory including inode structure;
- page fault happens at EXT4_SB(inode->i_sb) in ext4_mark_inode_dirty();
- system hangs with 'scheduling while atomic' BUG.

The patch implements a check for ext4_mb_new_preallocation() error code
and handles its failure as if ext4_mb_regular_allocator() fails.

Found by Linux File System Verification project (linuxtesting.org).

Signed-off-by: Alexey Khoroshilov <khoroshilov@ispras.ru>
---
 fs/ext4/mballoc.c |   15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
index b1ed9e0..95c6b0d 100644
--- a/fs/ext4/mballoc.c
+++ b/fs/ext4/mballoc.c
@@ -4401,17 +4401,18 @@ ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
 repeat:
 		/* allocate space in core */
 		*errp = ext4_mb_regular_allocator(ac);
+		if (!*errp) {
+			/* as we've just preallocated more space than
+			 * user requested orinally, we store allocated
+			 * space in a special descriptor */
+			if (ac->ac_status == AC_STATUS_FOUND &&
+					ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
+				*errp = ext4_mb_new_preallocation(ac);
+		}
 		if (*errp) {
 			ext4_discard_allocated_blocks(ac);
 			goto errout;
 		}

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

* Re: [PATCH] ext4: implement error handling of ext4_mb_new_preallocation()
  2013-05-05 20:51 [PATCH] ext4: implement error handling of ext4_mb_new_preallocation() Alexey Khoroshilov
@ 2013-05-06 19:24 ` Darrick J. Wong
  2013-06-17 13:17 ` Theodore Ts'o
  1 sibling, 0 replies; 3+ messages in thread
From: Darrick J. Wong @ 2013-05-06 19:24 UTC (permalink / raw)
  To: Alexey Khoroshilov
  Cc: Theodore Ts'o, Andreas Dilger, linux-ext4, linux-kernel,
	spruce-project

On Mon, May 06, 2013 at 12:51:15AM +0400, Alexey Khoroshilov wrote:
> If memory allocation in ext4_mb_new_group_pa() is failed,
> it returns error code, ext4_mb_new_preallocation() propages it,
> but ext4_mb_new_blocks() ignores it.
> 
> An observed result was:
> - allocation fail means ext4_mb_new_group_pa() does not update ext4_allocation_context;
> - ext4_mb_new_blocks() sets ext4_allocation_request->len (ar->len = ac->ac_b_ex.fe_len;)
>   to number of blocks preallocated (512) instead of number of blocks requested (1);
> - that activates update cycle in ext4_splice_branch():
>     for (i = 1; i < blks; i++) <-- blks is 512 instead of 1 here
>       *(where->p + i) = cpu_to_le32(current_block++);
> - it iterates 511 times and corrupts a chunk of memory including inode structure;
> - page fault happens at EXT4_SB(inode->i_sb) in ext4_mark_inode_dirty();
> - system hangs with 'scheduling while atomic' BUG.
> 
> The patch implements a check for ext4_mb_new_preallocation() error code
> and handles its failure as if ext4_mb_regular_allocator() fails.
> 
> Found by Linux File System Verification project (linuxtesting.org).
> 
> Signed-off-by: Alexey Khoroshilov <khoroshilov@ispras.ru>
> ---
>  fs/ext4/mballoc.c |   15 ++++++++-------
>  1 file changed, 8 insertions(+), 7 deletions(-)
> 
> diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
> index b1ed9e0..95c6b0d 100644
> --- a/fs/ext4/mballoc.c
> +++ b/fs/ext4/mballoc.c
> @@ -4401,17 +4401,18 @@ ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
>  repeat:
>  		/* allocate space in core */
>  		*errp = ext4_mb_regular_allocator(ac);
> +		if (!*errp) {
> +			/* as we've just preallocated more space than
> +			 * user requested orinally, we store allocated

"originally"

--D
> +			 * space in a special descriptor */
> +			if (ac->ac_status == AC_STATUS_FOUND &&
> +					ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
> +				*errp = ext4_mb_new_preallocation(ac);
> +		}
>  		if (*errp) {
>  			ext4_discard_allocated_blocks(ac);
>  			goto errout;
>  		}
> -
> -		/* as we've just preallocated more space than
> -		 * user requested orinally, we store allocated
> -		 * space in a special descriptor */
> -		if (ac->ac_status == AC_STATUS_FOUND &&
> -				ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
> -			ext4_mb_new_preallocation(ac);
>  	}
>  	if (likely(ac->ac_status == AC_STATUS_FOUND)) {
>  		*errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_clstrs);
> -- 
> 1.7.9.5
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [PATCH] ext4: implement error handling of ext4_mb_new_preallocation()
  2013-05-05 20:51 [PATCH] ext4: implement error handling of ext4_mb_new_preallocation() Alexey Khoroshilov
  2013-05-06 19:24 ` Darrick J. Wong
@ 2013-06-17 13:17 ` Theodore Ts'o
  1 sibling, 0 replies; 3+ messages in thread
From: Theodore Ts'o @ 2013-06-17 13:17 UTC (permalink / raw)
  To: Alexey Khoroshilov
  Cc: Andreas Dilger, linux-ext4, linux-kernel, spruce-project

On Mon, May 06, 2013 at 12:51:15AM +0400, Alexey Khoroshilov wrote:
> If memory allocation in ext4_mb_new_group_pa() is failed,
> it returns error code, ext4_mb_new_preallocation() propages it,
> but ext4_mb_new_blocks() ignores it.
> 
> An observed result was:
> - allocation fail means ext4_mb_new_group_pa() does not update ext4_allocation_context;
> - ext4_mb_new_blocks() sets ext4_allocation_request->len (ar->len = ac->ac_b_ex.fe_len;)
>   to number of blocks preallocated (512) instead of number of blocks requested (1);
> - that activates update cycle in ext4_splice_branch():
>     for (i = 1; i < blks; i++) <-- blks is 512 instead of 1 here
>       *(where->p + i) = cpu_to_le32(current_block++);
> - it iterates 511 times and corrupts a chunk of memory including inode structure;
> - page fault happens at EXT4_SB(inode->i_sb) in ext4_mark_inode_dirty();
> - system hangs with 'scheduling while atomic' BUG.
> 
> The patch implements a check for ext4_mb_new_preallocation() error code
> and handles its failure as if ext4_mb_regular_allocator() fails.
> 
> Found by Linux File System Verification project (linuxtesting.org).
> 
> Signed-off-by: Alexey Khoroshilov <khoroshilov@ispras.ru>

Hi Alexey,

Thanks for reporting this bug, and proposing a fix.  I've restructured
the patch slightly to make the flow of control slightly easier to
follow, and more consistent with the coding style in ext4.

Thanks again!

					- Ted

>From fa4f073ab981d4aabb61f9262405af53072a0d8d Mon Sep 17 00:00:00 2001
From: Alexey Khoroshilov <khoroshilov@ispras.ru>
Date: Mon, 17 Jun 2013 09:15:34 -0400
Subject: [PATCH] ext4: implement error handling of
 ext4_mb_new_preallocation()

If memory allocation in ext4_mb_new_group_pa() is failed,
it returns error code, ext4_mb_new_preallocation() propages it,
but ext4_mb_new_blocks() ignores it.

An observed result was:

- allocation fail means ext4_mb_new_group_pa() does not update
  ext4_allocation_context;

- ext4_mb_new_blocks() sets ext4_allocation_request->len (ar->len =
  ac->ac_b_ex.fe_len;) to number of blocks preallocated (512) instead
  of number of blocks requested (1);

- that activates update cycle in ext4_splice_branch():
    for (i = 1; i < blks; i++) <-- blks is 512 instead of 1 here
      *(where->p + i) = cpu_to_le32(current_block++);

- it iterates 511 times and corrupts a chunk of memory including inode
  structure;

- page fault happens at EXT4_SB(inode->i_sb) in ext4_mark_inode_dirty();

- system hangs with 'scheduling while atomic' BUG.

The patch implements a check for ext4_mb_new_preallocation() error
code and handles its failure as if ext4_mb_regular_allocator() fails.

Found by Linux File System Verification project (linuxtesting.org).

[ Patch restructed by tytso to make the flow of control easier to follow. ]

Signed-off-by: Alexey Khoroshilov <khoroshilov@ispras.ru>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
---
 fs/ext4/mballoc.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
index 1a9c22b..a9ff5e5 100644
--- a/fs/ext4/mballoc.c
+++ b/fs/ext4/mballoc.c
@@ -4406,17 +4406,20 @@ ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
 repeat:
 		/* allocate space in core */
 		*errp = ext4_mb_regular_allocator(ac);
-		if (*errp) {
-			ext4_discard_allocated_blocks(ac);
-			goto errout;
-		}
+		if (*errp)
+			goto discard_and_exit;
 
 		/* as we've just preallocated more space than
-		 * user requested orinally, we store allocated
+		 * user requested originally, we store allocated
 		 * space in a special descriptor */
 		if (ac->ac_status == AC_STATUS_FOUND &&
-				ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
-			ext4_mb_new_preallocation(ac);
+		    ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
+			*errp = ext4_mb_new_preallocation(ac);
+		if (*errp) {
+		discard_and_exit:
+			ext4_discard_allocated_blocks(ac);
+			goto errout;
+		}
 	}
 	if (likely(ac->ac_status == AC_STATUS_FOUND)) {
 		*errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_clstrs);
-- 
1.7.12.rc0.22.gcdd159b

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

end of thread, other threads:[~2013-06-17 13:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-05-05 20:51 [PATCH] ext4: implement error handling of ext4_mb_new_preallocation() Alexey Khoroshilov
2013-05-06 19:24 ` Darrick J. Wong
2013-06-17 13:17 ` Theodore Ts'o

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).