linux-ext4.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fs/ext4: fix potential endless loop in ext4_mb_discard_group_preallocations()
       [not found] <1441013790-87948-1-git-send-email-zhenzhang.zhang@huawei.com>
@ 2015-08-31  9:46 ` Zhang Zhen
  2015-08-31 22:34   ` Andreas Dilger
                     ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Zhang Zhen @ 2015-08-31  9:46 UTC (permalink / raw)
  To: tytso@mit.edu, Jan Kara, adilger.kernel; +Cc: linux-ext4@vger.kernel.org

In ext4_mb_discard_group_preallocations(), if free is always less than needed,
and some PAs are always used in every loop, it will be endless loop.

Here we pick a random value to limit the max number of loop.

Signed-off-by: Zhang Zhen <zhenzhang.zhang@huawei.com>
---
 fs/ext4/mballoc.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
index 34b610e..553fbde 100644
--- a/fs/ext4/mballoc.c
+++ b/fs/ext4/mballoc.c
@@ -3836,6 +3836,7 @@ ext4_mb_discard_group_preallocations(struct super_block *sb,
 	int err;
 	int busy = 0;
 	int free = 0;
+	int tried = 0;

 	mb_debug(1, "discard preallocation for group %u\n", group);

@@ -3886,9 +3887,11 @@ repeat:
 		list_add(&pa->u.pa_tmp_list, &list);
 	}

-	/* if we still need more blocks and some PAs were used, try again */
-	if (free < needed && busy) {
+	/* if we still need more blocks and some PAs were used, try again,
+	   here 20 is a ramdon value. */
+	if (free < needed && busy && tried < 20) {
 		busy = 0;
+		tried++;
 		ext4_unlock_group(sb, group);
 		cond_resched();
 		goto repeat;
-- 
1.9.1


.





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

* Re: [PATCH] fs/ext4: fix potential endless loop in ext4_mb_discard_group_preallocations()
  2015-08-31  9:46 ` [PATCH] fs/ext4: fix potential endless loop in ext4_mb_discard_group_preallocations() Zhang Zhen
@ 2015-08-31 22:34   ` Andreas Dilger
  2015-09-01  1:16     ` Zhang Zhen
  2015-09-01  6:41   ` [PATCH v2] " Zhang Zhen
  2015-09-16 19:45   ` [PATCH] " Jan Kara
  2 siblings, 1 reply; 6+ messages in thread
From: Andreas Dilger @ 2015-08-31 22:34 UTC (permalink / raw)
  To: Zhang Zhen
  Cc: tytso@mit.edu, Jan Kara, adilger.kernel,
	linux-ext4@vger.kernel.org

On Aug 31, 2015, at 3:46 AM, Zhang Zhen <zhenzhang.zhang@huawei.com> wrote:
> 
> In ext4_mb_discard_group_preallocations(), if free is always less
> than needed, and some PAs are always used in every loop, it will
> be endless loop.
> 
> Here we pick a random value to limit the max number of loop.
> 
> Signed-off-by: Zhang Zhen <zhenzhang.zhang@huawei.com>
> ---
> fs/ext4/mballoc.c | 7 +++++--
> 1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
> index 34b610e..553fbde 100644
> --- a/fs/ext4/mballoc.c
> +++ b/fs/ext4/mballoc.c
> @@ -3836,6 +3836,7 @@ ext4_mb_discard_group_preallocations(struct super_block *sb,
> 	int err;
> 	int busy = 0;
> 	int free = 0;
> +	int tried = 0;
> 
> 	mb_debug(1, "discard preallocation for group %u\n", group);
> 
> @@ -3886,9 +3887,11 @@ repeat:
> 		list_add(&pa->u.pa_tmp_list, &list);
> 	}
> 
> -	/* if we still need more blocks and some PAs were used, try again */
> -	if (free < needed && busy) {
> +	/* if we still need more blocks and some PAs were used, try again,
> +	   here 20 is a ramdon value. */

(typo) s/ramdon/random/

> +	if (free < needed && busy && tried < 20) {
> 		busy = 0;
> +		tried++;
> 		ext4_unlock_group(sb, group);
> 		cond_resched();
> 		goto repeat;

Looks OK otherwise.

Cheers, Andreas






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

* Re: [PATCH] fs/ext4: fix potential endless loop in ext4_mb_discard_group_preallocations()
  2015-08-31 22:34   ` Andreas Dilger
@ 2015-09-01  1:16     ` Zhang Zhen
  0 siblings, 0 replies; 6+ messages in thread
From: Zhang Zhen @ 2015-09-01  1:16 UTC (permalink / raw)
  To: Andreas Dilger
  Cc: tytso@mit.edu, Jan Kara, adilger.kernel,
	linux-ext4@vger.kernel.org

On 2015/9/1 6:34, Andreas Dilger wrote:
> On Aug 31, 2015, at 3:46 AM, Zhang Zhen <zhenzhang.zhang@huawei.com> wrote:
>>
>> In ext4_mb_discard_group_preallocations(), if free is always less
>> than needed, and some PAs are always used in every loop, it will
>> be endless loop.
>>
>> Here we pick a random value to limit the max number of loop.
>>
>> Signed-off-by: Zhang Zhen <zhenzhang.zhang@huawei.com>
>> ---
>> fs/ext4/mballoc.c | 7 +++++--
>> 1 file changed, 5 insertions(+), 2 deletions(-)
>>
>> diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
>> index 34b610e..553fbde 100644
>> --- a/fs/ext4/mballoc.c
>> +++ b/fs/ext4/mballoc.c
>> @@ -3836,6 +3836,7 @@ ext4_mb_discard_group_preallocations(struct super_block *sb,
>> 	int err;
>> 	int busy = 0;
>> 	int free = 0;
>> +	int tried = 0;
>>
>> 	mb_debug(1, "discard preallocation for group %u\n", group);
>>
>> @@ -3886,9 +3887,11 @@ repeat:
>> 		list_add(&pa->u.pa_tmp_list, &list);
>> 	}
>>
>> -	/* if we still need more blocks and some PAs were used, try again */
>> -	if (free < needed && busy) {
>> +	/* if we still need more blocks and some PAs were used, try again,
>> +	   here 20 is a ramdon value. */
> 
> (typo) s/ramdon/random/
> 
My mistake, thanks!

>> +	if (free < needed && busy && tried < 20) {
>> 		busy = 0;
>> +		tried++;
>> 		ext4_unlock_group(sb, group);
>> 		cond_resched();
>> 		goto repeat;
> 
> Looks OK otherwise.
> 
> Cheers, Andreas
> 
> 
> 
> 
> 
> 
> 



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

* [PATCH v2] fs/ext4: fix potential endless loop in ext4_mb_discard_group_preallocations()
  2015-08-31  9:46 ` [PATCH] fs/ext4: fix potential endless loop in ext4_mb_discard_group_preallocations() Zhang Zhen
  2015-08-31 22:34   ` Andreas Dilger
@ 2015-09-01  6:41   ` Zhang Zhen
  2015-09-16 19:45   ` [PATCH] " Jan Kara
  2 siblings, 0 replies; 6+ messages in thread
From: Zhang Zhen @ 2015-09-01  6:41 UTC (permalink / raw)
  To: tytso@mit.edu, adilger.kernel; +Cc: linux-ext4@vger.kernel.org

In ext4_mb_discard_group_preallocations(), if free is always less than needed,
and some PAs are always used in every loop, it will be endless loop.

Here we pick a random value to limit the max number of loop.

Change v1 -> v2:
- fix typo error, s/ramdon/random/

Signed-off-by: Zhang Zhen <zhenzhang.zhang@huawei.com>
Acked-by: Andreas Dilger <adilger.kernel@dilger.ca>
---
 fs/ext4/mballoc.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
index 34b610e..553fbde 100644
--- a/fs/ext4/mballoc.c
+++ b/fs/ext4/mballoc.c
@@ -3836,6 +3836,7 @@ ext4_mb_discard_group_preallocations(struct super_block *sb,
 	int err;
 	int busy = 0;
 	int free = 0;
+	int tried = 0;

 	mb_debug(1, "discard preallocation for group %u\n", group);

@@ -3886,9 +3887,11 @@ repeat:
 		list_add(&pa->u.pa_tmp_list, &list);
 	}

-	/* if we still need more blocks and some PAs were used, try again */
-	if (free < needed && busy) {
+	/* if we still need more blocks and some PAs were used, try again,
+	   here 20 is a random value. */
+	if (free < needed && busy && tried < 20) {
 		busy = 0;
+		tried++;
 		ext4_unlock_group(sb, group);
 		cond_resched();
 		goto repeat;
-- 
1.9.1


.







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

* Re: [PATCH] fs/ext4: fix potential endless loop in ext4_mb_discard_group_preallocations()
  2015-08-31  9:46 ` [PATCH] fs/ext4: fix potential endless loop in ext4_mb_discard_group_preallocations() Zhang Zhen
  2015-08-31 22:34   ` Andreas Dilger
  2015-09-01  6:41   ` [PATCH v2] " Zhang Zhen
@ 2015-09-16 19:45   ` Jan Kara
  2015-09-17  1:27     ` Zhang Zhen
  2 siblings, 1 reply; 6+ messages in thread
From: Jan Kara @ 2015-09-16 19:45 UTC (permalink / raw)
  To: Zhang Zhen
  Cc: tytso@mit.edu, Jan Kara, adilger.kernel,
	linux-ext4@vger.kernel.org

On Mon 31-08-15 17:46:24, Zhang Zhen wrote:
> In ext4_mb_discard_group_preallocations(), if free is always less than needed,
> and some PAs are always used in every loop, it will be endless loop.
> 
> Here we pick a random value to limit the max number of loop.

Were you able to trigger this in practice or is it just a theoretical
concern?

My slight concern is that in theory we could prematurely declare ENOSPC
with this patch since ext4_mb_discard_preallocations() doesn't reliably
discard all the preallocations anymore. But probably that's acceptable.
But we should add a comment before ext4_mb_discard_group_preallocations()
saying that the functions needn't free all the preallocations.

								Honza
> 
> Signed-off-by: Zhang Zhen <zhenzhang.zhang@huawei.com>
> ---
>  fs/ext4/mballoc.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
> index 34b610e..553fbde 100644
> --- a/fs/ext4/mballoc.c
> +++ b/fs/ext4/mballoc.c
> @@ -3836,6 +3836,7 @@ ext4_mb_discard_group_preallocations(struct super_block *sb,
>  	int err;
>  	int busy = 0;
>  	int free = 0;
> +	int tried = 0;
> 
>  	mb_debug(1, "discard preallocation for group %u\n", group);
> 
> @@ -3886,9 +3887,11 @@ repeat:
>  		list_add(&pa->u.pa_tmp_list, &list);
>  	}
> 
> -	/* if we still need more blocks and some PAs were used, try again */
> -	if (free < needed && busy) {
> +	/* if we still need more blocks and some PAs were used, try again,
> +	   here 20 is a ramdon value. */
> +	if (free < needed && busy && tried < 20) {
>  		busy = 0;
> +		tried++;
>  		ext4_unlock_group(sb, group);
>  		cond_resched();
>  		goto repeat;
> -- 
> 1.9.1
> 
> 
> .
> 
> 
> 
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

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

* Re: [PATCH] fs/ext4: fix potential endless loop in ext4_mb_discard_group_preallocations()
  2015-09-16 19:45   ` [PATCH] " Jan Kara
@ 2015-09-17  1:27     ` Zhang Zhen
  0 siblings, 0 replies; 6+ messages in thread
From: Zhang Zhen @ 2015-09-17  1:27 UTC (permalink / raw)
  To: Jan Kara; +Cc: tytso@mit.edu, adilger.kernel, linux-ext4@vger.kernel.org

On 2015/9/17 3:45, Jan Kara wrote:
> On Mon 31-08-15 17:46:24, Zhang Zhen wrote:
>> In ext4_mb_discard_group_preallocations(), if free is always less than needed,
>> and some PAs are always used in every loop, it will be endless loop.
>>
>> Here we pick a random value to limit the max number of loop.
> 
> Were you able to trigger this in practice or is it just a theoretical
> concern?
> 
We found the problem in a stress test.
It lead to system hungtask.

Thanks!
> My slight concern is that in theory we could prematurely declare ENOSPC
> with this patch since ext4_mb_discard_preallocations() doesn't reliably
> discard all the preallocations anymore. But probably that's acceptable.
> But we should add a comment before ext4_mb_discard_group_preallocations()
> saying that the functions needn't free all the preallocations.
> 
> 								Honza
>>
>> Signed-off-by: Zhang Zhen <zhenzhang.zhang@huawei.com>
>> ---
>>  fs/ext4/mballoc.c | 7 +++++--
>>  1 file changed, 5 insertions(+), 2 deletions(-)
>>
>> diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
>> index 34b610e..553fbde 100644
>> --- a/fs/ext4/mballoc.c
>> +++ b/fs/ext4/mballoc.c
>> @@ -3836,6 +3836,7 @@ ext4_mb_discard_group_preallocations(struct super_block *sb,
>>  	int err;
>>  	int busy = 0;
>>  	int free = 0;
>> +	int tried = 0;
>>
>>  	mb_debug(1, "discard preallocation for group %u\n", group);
>>
>> @@ -3886,9 +3887,11 @@ repeat:
>>  		list_add(&pa->u.pa_tmp_list, &list);
>>  	}
>>
>> -	/* if we still need more blocks and some PAs were used, try again */
>> -	if (free < needed && busy) {
>> +	/* if we still need more blocks and some PAs were used, try again,
>> +	   here 20 is a ramdon value. */
>> +	if (free < needed && busy && tried < 20) {
>>  		busy = 0;
>> +		tried++;
>>  		ext4_unlock_group(sb, group);
>>  		cond_resched();
>>  		goto repeat;
>> -- 
>> 1.9.1
>>
>>
>> .
>>
>>
>>
>>



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

end of thread, other threads:[~2015-09-17  1:29 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <1441013790-87948-1-git-send-email-zhenzhang.zhang@huawei.com>
2015-08-31  9:46 ` [PATCH] fs/ext4: fix potential endless loop in ext4_mb_discard_group_preallocations() Zhang Zhen
2015-08-31 22:34   ` Andreas Dilger
2015-09-01  1:16     ` Zhang Zhen
2015-09-01  6:41   ` [PATCH v2] " Zhang Zhen
2015-09-16 19:45   ` [PATCH] " Jan Kara
2015-09-17  1:27     ` Zhang Zhen

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