Skip to contentSkip to conversation

fsx: fix infinite/too long loops when generating ranges for clone operations

PATCH In review [email protected] 2 participants 2 messages Apr 8, 2020 – Apr 17, 2020 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 2/4 fsx: fix infinite/too long loops when generating ranges for clone operations

From: Filipe Manana <[email protected]>

While running generic/457 I've had fsx taking a lot of CPU time and not making any progress for over an hour. Attaching gdb to the fsx process revealed that fsx was in the loop that generates the ranges for a clone operation, in particular the loop seemed to never end because the range defined by 'offset2' kept overlapping with the range defined by 'offset'. So far this happened two times in one of my test VMs with generic/457.

Fix this by breaking out of the loop after trying 30 times, like we currently do for dedupe operations, which results in logging the operation as skipped.

---
 ltp/fsx.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)
ltp/fsx.c +18 -10
index fa383c94..5949ebf0 100644
@@ -2004,16 +2004,24 @@ test(void)
 			keep_size = random() % 2;
 		break;
 	case OP_CLONE_RANGE:
-		TRIM_OFF_LEN(offset, size, file_size);
-		offset = offset & ~(block_size - 1);
-		size = size & ~(block_size - 1);
-		do {
-			offset2 = random();
-			TRIM_OFF(offset2, maxfilelen);
-			offset2 = offset2 & ~(block_size - 1);
-		} while (range_overlaps(offset, offset2, size) ||
-			 offset2 + size > maxfilelen);
-		break;
+		{
+			int tries = 0;
+
+			TRIM_OFF_LEN(offset, size, file_size);
+			offset = offset & ~(block_size - 1);
+			size = size & ~(block_size - 1);
+			do {
+				if (tries++ >= 30) {
+					size = 0;
+					break;
+				}
+				offset2 = random();
+				TRIM_OFF(offset2, maxfilelen);
+				offset2 = offset2 & ~(block_size - 1);
+			} while (range_overlaps(offset, offset2, size) ||
+				 offset2 + size > maxfilelen);
+			break;
+		}
 	case OP_DEDUPE_RANGE:
 		{
 			int tries = 0;
Signature · 1 line
2.11.0
Reviewed-by on v1 2/4
#1 lore raw
Quoted 9 lines · On Wed, Apr 08, 2020 at 11:36:04AM +0100, [email protected] wrote:

From: Filipe Manana <[email protected]>

While running generic/457 I've had fsx taking a lot of CPU time and not making any progress for over an hour. Attaching gdb to the fsx process revealed that fsx was in the loop that generates the ranges for a clone operation, in particular the loop seemed to never end because the range defined by 'offset2' kept overlapping with the range defined by 'offset'. So far this happened two times in one of my test VMs with generic/457.

Quoted 6 lines

Fix this by breaking out of the loop after trying 30 times, like we currently do for dedupe operations, which results in logging the operation as skipped.

---

Quoted 45 lines
 ltp/fsx.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)
ltp/fsx.c +18 -10
index fa383c94..5949ebf0 100644
@@ -2004,16 +2004,24 @@ test(void)
 			keep_size = random() % 2;
 		break;
 	case OP_CLONE_RANGE:
-		TRIM_OFF_LEN(offset, size, file_size);
-		offset = offset & ~(block_size - 1);
-		size = size & ~(block_size - 1);
-		do {
-			offset2 = random();
-			TRIM_OFF(offset2, maxfilelen);
-			offset2 = offset2 & ~(block_size - 1);
-		} while (range_overlaps(offset, offset2, size) ||
-			 offset2 + size > maxfilelen);
-		break;
+		{
+			int tries = 0;
+
+			TRIM_OFF_LEN(offset, size, file_size);
+			offset = offset & ~(block_size - 1);
+			size = size & ~(block_size - 1);
+			do {
+				if (tries++ >= 30) {
+					size = 0;
+					break;
+				}
+				offset2 = random();
+				TRIM_OFF(offset2, maxfilelen);
+				offset2 = offset2 & ~(block_size - 1);
+			} while (range_overlaps(offset, offset2, size) ||
+				 offset2 + size > maxfilelen);
+			break;
+		}
 	case OP_DEDUPE_RANGE:
 		{
 			int tries = 0;
Signature · 1 line
2.11.0