Skip to contentSkip to conversation

fs/ext4: fix potential endless loop in ext4_mb_discard_group_preallocations()

PATCH Superseded Zhang Zhen 3 participants 6 messages Aug 31, 2015 – Sep 17, 2015 1 message not in the archive ext4 file system

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Zhang Zhen

author
#1 lore raw

PATCH 1/1 fs/ext4: fix potential endless loop in ext4_mb_discard_group_preallocations()

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.

---
 fs/ext4/mballoc.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/ext4/mballoc.c +5 -2
index 34b610e..553fbde 100644
@@ -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;
Signature · 4 lines
1.9.1


.
#2 lore raw
Quoted 25 lines · Zhang Zhen

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.

---
fs/ext4/mballoc.c | 7 +++++--

1 file changed, 5 insertions(+), 2 deletions(-)
fs/ext4/mballoc.c +1 -0
index 34b610e..553fbde 100644
@@ -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);
 

fs/ext4/mballoc.c repeat: · line 3891

Quoted 7 lines
fs/ext4/mballoc.c +2 -2
@@ -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/

fs/ext4/mballoc.c repeat: · line 3897

Quoted 6 lines
fs/ext4/mballoc.c +2 -0
+	if (free < needed && busy && tried < 20) {
 		busy = 0;
+		tried++;
 		ext4_unlock_group(sb, group);
 		cond_resched();
 		goto repeat;

Looks OK otherwise.

Cheers, Andreas

author
#3 lore raw
Quoted 29 lines · Andreas Dilger
Quoted 28 lines · Zhang Zhen

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.

---
fs/ext4/mballoc.c | 7 +++++--

1 file changed, 5 insertions(+), 2 deletions(-)
fs/ext4/mballoc.c +1 -0
index 34b610e..553fbde 100644
@@ -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);
 	}
 
Quoted 7 lines

fs/ext4/mballoc.c repeat: · line 3891

Quoted 4 lines
fs/ext4/mballoc.c +2 -2
-	/* 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!

Quoted 17 lines

fs/ext4/mballoc.c repeat: · line 3897

Quoted 6 lines
fs/ext4/mballoc.c +2 -0
+	if (free < needed && busy && tried < 20) {
 		busy = 0;
+		tried++;
 		ext4_unlock_group(sb, group);
 		cond_resched();
 		goto repeat;

Looks OK otherwise.

Cheers, Andreas

author
#4 lore raw

PATCH v2 1/1 fs/ext4: fix potential endless loop in ext4_mb_discard_group_preallocations()

In reply to Zhang Zhen

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/

---
 fs/ext4/mballoc.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/ext4/mballoc.c +5 -2
index 34b610e..553fbde 100644
@@ -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;
Signature · 4 lines
1.9.1


.
reviewer
#5 lore raw
In reply to Zhang Zhen
Quoted 4 lines · Zhang Zhen

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

Quoted 41 lines
---
 fs/ext4/mballoc.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/ext4/mballoc.c +5 -2
index 34b610e..553fbde 100644
@@ -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;
Signature · 4 lines
1.9.1


.
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author
#6 lore raw
Quoted 9 lines · Jan Kara
Quoted 4 lines · Zhang Zhen

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!

Quoted 48 lines

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

Quoted 41 lines
---
 fs/ext4/mballoc.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/ext4/mballoc.c +5 -2
index 34b610e..553fbde 100644
@@ -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;
Signature · 4 lines
1.9.1


.