Skip to contentSkip to conversation

lib: Use exponential-backoff polling to wait for loop device nodes

PATCH Superseded Wake Liu via ltp 4 participants 30 messages Aug 3 – Aug 12 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib: Use exponential-backoff polling to wait for loop device nodes

On systems where loop device node creation is asynchronous (such as Android containers or systems with slow udev startup), calling stat() or open() immediately after LOOP_CTL_GET_FREE can transiently fail because the device file (e.g. /dev/loopX) has not been fully populated in time.

Introduce an exponential-backoff retry loop in both tst_find_free_loopdev() and tst_attach_device() (starting at 1ms delay, doubling each try, capped at 100ms) to wait for the device node to be successfully populated.

This improves the robustness of loop device allocations on asynchronous virtualized environments while minimizing unnecessary delays on responsive systems.

---
 lib/tst_device.c | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)
lib/tst_device.c +25 -4
index 744173ffef..6e3c6fef21 100644
@@ -82,7 +82,7 @@ static int set_dev_path(char *dev, char *path, size_t path_len)
 
 int tst_find_free_loopdev(char *path, size_t path_len)
 {
-	int ctl_fd, dev_fd, rc, i;
+	int ctl_fd, dev_fd, rc, i, path_set;
 	struct loop_info loopinfo;
 	char buf[PATH_MAX];
 
@@ -93,8 +93,20 @@ int tst_find_free_loopdev(char *path, size_t path_len)
 		rc = ioctl(ctl_fd, LOOP_CTL_GET_FREE);
 		close(ctl_fd);
 		if (rc >= 0) {
-			if (path && set_dev_loop_path(rc, path, path_len))
-				tst_brkm(TBROK, NULL, "Could not stat loop device %i", rc);
+			if (path) {
+				unsigned int usec = 1000; /* start with 1ms */
+
+				for (i = 0; i < 50; i++) {
+					path_set = set_dev_loop_path(rc, path, path_len);
+					if (!path_set)
+						break;
+					usleep(usec);
+					if (usec < 100000) /* cap backoff at 100ms */
+						usec *= 2;
+				}
+				if (path_set)
+					tst_brkm(TBROK, NULL, "Could not stat loop device %i", rc);
+			}
 			tst_resm(TINFO, "Found free device %d '%s'",
 				rc, path ?: "");
 			return rc;
@@ -155,13 +167,22 @@ int tst_attach_device(const char *dev, const char *file)
 {
 	int dev_fd, file_fd;
 	struct loop_info loopinfo;
+	int attach_tries = 20;
+	unsigned int usec = 1000; /* start with 1ms */
 
 	if (strlen(file) >= LO_NAME_SIZE) {
 		tst_brkm(TBROK, NULL, "Device name can't be longer than %u chars",
 			 LO_NAME_SIZE);
 	}
 
-	dev_fd = open(dev, O_RDWR);
+	while (attach_tries--) {
+		dev_fd = open(dev, O_RDWR);
+		if (dev_fd >= 0)
+			break;
+		usleep(usec);
+		if (usec < 100000) /* cap backoff at 100ms */
+			usec *= 2;
+	}
 	if (dev_fd < 0) {
 		tst_resm(TWARN | TERRNO, "open('%s', O_RDWR) failed", dev);
 		return 1;
Signature · 5 lines
2.55.0.508.g3f0d502094-goog


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi Wake,

Quoted 5 lines · Wake Liu

lib: Use exponential-backoff polling to wait for loop device nodes

					usleep(usec);
					if (usec < 100000) /* cap backoff at 100ms */
						usec *= 2;

Could this clamp the doubled value to 100000 and only sleep when another attempt remains? When usec is 64000, this condition doubles it to 128000, so the advertised 100 ms cap is exceeded. The loops also sleep after the final failed probe. As a result, the 50-attempt stat path can sleep for 5.631 seconds and the 20-attempt open path for 1.791 seconds.

Verdict - Needs revision

---
Note:

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

PATCH v2 1/1 lib: Use backoff polling to wait for loop device nodes

In reply to Wake Liu via ltp

From: Wake Liu via ltp <[email protected]>

On systems where loop device node creation is asynchronous (such as Android containers or systems with slow udev startup), calling stat() or open() immediately after LOOP_CTL_GET_FREE can transiently fail because the device file (e.g. /dev/loopX) has not been fully populated in time.

Introduce an exponential-backoff retry loop in both tst_find_free_loopdev() and tst_attach_device() (starting at 1ms delay, doubling each try, capped at 100ms) to wait for the device node to be successfully populated.

This improves the robustness of loop device allocations on asynchronous virtualized environments while minimizing unnecessary delays on responsive systems.

---
v1 -> v2:
- Strictly clamp backoff delay to 100ms (prevent overflow exceeding cap).
- Skip usleep on final loop iteration to avoid unnecessary idle delay.
- Reduce max attempts to 30 (stat) and 15 (open) to bound worst-case timeout.

 lib/tst_device.c | 32 +++++++++++++++++++++++++++-----
lib/tst_device.c +21 -10
index 744173ffef..19e1a8a25c 100644
@@ -82,7 +82,7 @@ static int set_dev_loop_path(int rc, char *path, size_t path_len)
 
 int tst_find_free_loopdev(char *path, size_t path_len)
 {
-	int ctl_fd, dev_fd, rc, i;
+	int ctl_fd, dev_fd, rc, i, path_set;
 	struct loop_info loopinfo;
 	char buf[PATH_MAX];
 
@@ -94,8 +94,18 @@ int tst_find_free_loopdev(char *path, size_t path_len)
 		if (rc >= 0) {
 			if (path) {
-				if (set_dev_loop_path(rc, path, path_len))
-					tst_brkm(TBROK, NULL, "Could not stat loop device %i", rc);
+				unsigned int usec = 1000; /* start with 1ms */
+
+				for (i = 0; i < 30; i++) {
+					path_set = set_dev_loop_path(rc, path, path_len);
+					if (!path_set)
+						break;
+					if (i < 29) {
+						usleep(usec);
+						usec = usec * 2 < 100000 ? usec * 2 : 100000;
+					}
+				}
+				if (path_set)
+					tst_brkm(TBROK, NULL, "Could not stat loop device %i", rc);
 			}
 			tst_resm(TINFO, "Found free device %d '%s'",
 				rc, path ?: "");
@@ -166,9 +176,8 @@ int tst_find_free_loopdev(char *path, size_t path_len)
 
 int tst_attach_device(const char *dev, const char *file)
 {
-	int dev_fd, file_fd;
-	struct loop_info loopinfo;
-	int attach_tries = 20;
+	int dev_fd, file_fd, i;
+	struct loop_info loopinfo;
 	unsigned int usec = 1000; /* start with 1ms */
 
 	if (strlen(file) >= LO_NAME_SIZE) {
@@ -175,13 +184,14 @@ int tst_attach_device(const char *dev, const char *file)
 			 LO_NAME_SIZE);
 	}
 
-	while (attach_tries--) {
+	for (i = 0; i < 15; i++) {
 		dev_fd = open(dev, O_RDWR);
 		if (dev_fd >= 0)
 			break;
-		usleep(usec);
-		if (usec < 100000) /* cap backoff at 100ms */
-			usec *= 2;
+		if (i < 14) {
+			usleep(usec);
+			usec = usec * 2 < 100000 ? usec * 2 : 100000;
+		}
 	}
 	if (dev_fd < 0) {
 		tst_resm(TWARN | TERRNO, "open('%s', O_RDWR) failed", dev);
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Re: [LTP] [PATCH v2] lib: Use backoff polling to wait for loop device nodes

Hi Wake,

Quoted 45 lines · Wake Liu via ltp

From: Wake Liu via ltp <[email protected]>

On systems where loop device node creation is asynchronous (such as Android containers or systems with slow udev startup), calling stat() or open() immediately after LOOP_CTL_GET_FREE can transiently fail because the device file (e.g. /dev/loopX) has not been fully populated in time.

Introduce an exponential-backoff retry loop in both tst_find_free_loopdev() and tst_attach_device() (starting at 1ms delay, doubling each try, capped at 100ms) to wait for the device node to be successfully populated.

This improves the robustness of loop device allocations on asynchronous virtualized environments while minimizing unnecessary delays on responsive systems.

---
v1 -> v2:
- Strictly clamp backoff delay to 100ms (prevent overflow exceeding cap).
- Skip usleep on final loop iteration to avoid unnecessary idle delay.
- Reduce max attempts to 30 (stat) and 15 (open) to bound worst-case timeout.

 lib/tst_device.c | 32 +++++++++++++++++++++++++++-----
lib/tst_device.c +2 -3
index 744173ffef..19e1a8a25c 100644
@@ -82,7 +82,7 @@ static int set_dev_loop_path(int rc, char *path, size_t path_len)
 
 int tst_find_free_loopdev(char *path, size_t path_len)
 {
-	int ctl_fd, dev_fd, rc, i;
+	int ctl_fd, dev_fd, rc, i, path_set;
 	struct loop_info loopinfo;
 	char buf[PATH_MAX];
 
@@ -94,8 +94,18 @@ int tst_find_free_loopdev(char *path, size_t path_len)
 		if (rc >= 0) {
 			if (path) {
-				if (set_dev_loop_path(rc, path, path_len))
-					tst_brkm(TBROK, NULL, "Could not stat loop device %i", rc);
+				unsigned int usec = 1000; /* start with 1ms */

lib/tst_device.c int tst_find_free_loopdev(char *path, size_t path_len) · line 106

Quoted 10 lines
lib/tst_device.c +10 -0
+
+				for (i = 0; i < 30; i++) {
+					path_set = set_dev_loop_path(rc, path, path_len);
+					if (!path_set)
+						break;
+					if (i < 29) {
+						usleep(usec);
+						usec = usec * 2 < 100000 ? usec * 2 : 100000;
+					}
+				}

LTP has already provided the exponential-backoff macro in tst_common.h

TST_RETRY_FN_EXP_BACKOFF()

Maybe you can reuse it directly?

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

PATCH v3 1/1 lib: Use backoff polling to wait for loop device nodes

On systems where loop device node creation is asynchronous (such as Android containers or systems with slow udev startup), calling stat() or open() immediately after LOOP_CTL_GET_FREE can transiently fail because the device file (e.g. /dev/loopX) has not been fully populated in time.

Introduce an exponential-backoff retry loop in both tst_find_free_loopdev() and tst_attach_device() (starting at 1ms delay, doubling each try, capped at 100ms) to wait for the device node to be successfully populated.

This improves the robustness of loop device allocations on asynchronous virtualized environments while minimizing unnecessary delays on responsive systems.

---
v2 -> v3:
- Reuse LTP native TST_RETRY_FN_EXP_BACKOFF() macro in tst_common.h to simplify code.

v1 -> v2:
- Strictly clamp backoff delay to 100ms (prevent overflow exceeding cap).
- Skip usleep on final loop iteration to avoid unnecessary idle delay.
- Reduce max attempts to 30 (stat) and 15 (open) to bound worst-case timeout.

 lib/tst_device.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
lib/tst_device.c +9 -4
index d3c53a1a1..a3c0992e0 100644
@@ -82,7 +82,7 @@ static int set_dev_path(char *dev, char *path, size_t path_len)
 
 int tst_find_free_loopdev(char *path, size_t path_len)
 {
-	int ctl_fd, dev_fd, rc, i;
+	int ctl_fd, dev_fd, rc, i, path_set;
 	struct loop_info loopinfo;
 	char buf[PATH_MAX];
 
@@ -93,8 +93,13 @@ int tst_find_free_loopdev(char *path, size_t path_len)
 		rc = ioctl(ctl_fd, LOOP_CTL_GET_FREE);
 		close(ctl_fd);
 		if (rc >= 0) {
-			if (path && set_dev_loop_path(rc, path, path_len))
-				tst_brkm(TBROK, NULL, "Could not stat loop device %i", rc);
+			if (path) {
+				path_set = TST_RETRY_FN_EXP_BACKOFF(
+					set_dev_loop_path(rc, path, path_len),
+					TST_RETVAL_EQ0, 1);
+				if (path_set)
+					tst_brkm(TBROK, NULL, "Could not stat loop device %i", rc);
+			}
 			tst_resm(TINFO, "Found free device %d '%s'",
 				rc, path ?: "");
 			return rc;
@@ -156,7 +161,7 @@ int tst_attach_device(const char *dev, const char *file)
 	int dev_fd, file_fd;
 	struct loop_info loopinfo;
 
-	dev_fd = open(dev, O_RDWR);
+	dev_fd = TST_RETRY_FN_EXP_BACKOFF(open(dev, O_RDWR), TST_RETVAL_GE0, 1);
 	if (dev_fd < 0) {
 		tst_resm(TWARN | TERRNO, "open('%s', O_RDWR) failed", dev);
 		return 1;
Signature · 5 lines
2.55.0.571.g244d577d93-goog


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

Re: [LTP] [PATCH v3] lib: Use backoff polling to wait for loop device nodes

Hi Wake,

v3 looks good overall, but it needs a rebase onto the latest LTP HEAD, otherwise it doesn't apply cleanly here.

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw

PATCH v4 1/1 lib: Use backoff polling to wait for loop device nodes

On systems where loop device node creation is asynchronous (such as Android containers or systems with slow udev startup), calling stat() or open() immediately after LOOP_CTL_GET_FREE can transiently fail because the device file (e.g. /dev/loopX) has not been fully populated in time.

Introduce an exponential-backoff retry loop in both tst_find_free_loopdev() and tst_attach_device() (starting at 1ms delay, doubling each try, capped at 100ms) to wait for the device node to be successfully populated.

This improves the robustness of loop device allocations on asynchronous virtualized environments while minimizing unnecessary delays on responsive systems.

---
v3 -> v4:
- Rebase onto the latest LTP HEAD.
- Remove self-referencing Link in commit message.

v2 -> v3:
- Reuse LTP native TST_RETRY_FN_EXP_BACKOFF() macro in tst_common.h to simplify code.

v1 -> v2:
- Strictly clamp backoff delay to 100ms (prevent overflow exceeding cap).
- Skip usleep on final loop iteration to avoid unnecessary idle delay.
- Reduce max attempts to 30 (stat) and 15 (open) to bound worst-case timeout.

 lib/tst_device.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
lib/tst_device.c +9 -4
index 744173ffe..00304711b 100644
@@ -82,7 +82,7 @@ static int set_dev_path(char *dev, char *path, size_t path_len)
 
 int tst_find_free_loopdev(char *path, size_t path_len)
 {
-	int ctl_fd, dev_fd, rc, i;
+	int ctl_fd, dev_fd, rc, i, path_set;
 	struct loop_info loopinfo;
 	char buf[PATH_MAX];
 
@@ -93,8 +93,13 @@ int tst_find_free_loopdev(char *path, size_t path_len)
 		rc = ioctl(ctl_fd, LOOP_CTL_GET_FREE);
 		close(ctl_fd);
 		if (rc >= 0) {
-			if (path && set_dev_loop_path(rc, path, path_len))
-				tst_brkm(TBROK, NULL, "Could not stat loop device %i", rc);
+			if (path) {
+				path_set = TST_RETRY_FN_EXP_BACKOFF(
+					set_dev_loop_path(rc, path, path_len),
+					TST_RETVAL_EQ0, 1);
+				if (path_set)
+					tst_brkm(TBROK, NULL, "Could not stat loop device %i", rc);
+			}
 			tst_resm(TINFO, "Found free device %d '%s'",
 				rc, path ?: "");
 			return rc;
@@ -161,7 +166,7 @@ int tst_attach_device(const char *dev, const char *file)
 			 LO_NAME_SIZE);
 	}
 
-	dev_fd = open(dev, O_RDWR);
+	dev_fd = TST_RETRY_FN_EXP_BACKOFF(open(dev, O_RDWR), TST_RETVAL_GE0, 1);
 	if (dev_fd < 0) {
 		tst_resm(TWARN | TERRNO, "open('%s', O_RDWR) failed", dev);
 		return 1;
Signature · 5 lines
2.55.0.571.g244d577d93-goog


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw

Re: [LTP] lib: Use backoff polling to wait for loop device nodes

Hi Wake,

lib/tst_device.c int tst_find_free_loopdev(char *path, size_t path_len) · line 99

Quoted 5 lines · Wake Liu

lib: Use backoff polling to wait for loop device nodes

lib/tst_device.c +3 -0
+				path_set = TST_RETRY_FN_EXP_BACKOFF(
+					set_dev_loop_path(rc, path, path_len),
+					TST_RETVAL_EQ0, 1);

Could the declaration of tst_multiply_timeout() be made available to this legacy-API library, or could polling avoid this macro? The macro expands to a call to tst_multiply_timeout(), but test.h does not declare it. GCC reports an implicit declaration and Clang rejects both new call sites under C99:

Quoted 5 lines

error: call to undeclared function 'tst_multiply_timeout'; ISO C99 and later do not support implicit function declarations

tst_find_free_loopdev() and tst_attach_device() (starting at 1ms delay, doubling each try, capped at 100ms) to wait for the device node to be successfully populated.

Could this timing description be corrected or the requested timing be implemented? TST_RETRY_FN_EXP_BACKOFF(..., 1) starts at one microsecond and uses one second as its maximum-delay threshold, subject to tst_multiply_timeout().

Verdict - Needs revision

---
Note:

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#8 lore raw

Re: [LTP] lib: Use backoff polling to wait for loop device nodes

Hi Wake,

The AI comments make sense, I overlooked them before ack your patch.

TST_RETRY_FN_EXP_BACKOFF() is not a drop-in replacement here: it depends on tst_multiply_timeout(), which is not declared by test.h for legacy-API users, and its timing semantics do not match the commit message. In particular, the argument 1 is 1us, not 1ms, and the cap is the macro's own timeout-adjusted threshold rather than 100ms.

Quoted 39 lines · On Wed, Aug 05, 2026 at 04:09:54AM +0000, [email protected]…

Hi Wake,

lib/tst_device.c int tst_find_free_loopdev(char *path, size_t path_len) · line 99

Quoted 5 lines · Wake Liu

lib: Use backoff polling to wait for loop device nodes

lib/tst_device.c +3 -0
+				path_set = TST_RETRY_FN_EXP_BACKOFF(
+					set_dev_loop_path(rc, path, path_len),
+					TST_RETVAL_EQ0, 1);

Could the declaration of tst_multiply_timeout() be made available to this legacy-API library, or could polling avoid this macro? The macro expands to a call to tst_multiply_timeout(), but test.h does not declare it. GCC reports an implicit declaration and Clang rejects both new call sites under C99:

Quoted 5 lines

error: call to undeclared function 'tst_multiply_timeout'; ISO C99 and later do not support implicit function declarations

tst_find_free_loopdev() and tst_attach_device() (starting at 1ms delay, doubling each try, capped at 100ms) to wait for the device node to be successfully populated.

Could this timing description be corrected or the requested timing be implemented? TST_RETRY_FN_EXP_BACKOFF(..., 1) starts at one microsecond and uses one second as its maximum-delay threshold, subject to tst_multiply_timeout().

Verdict - Needs revision

---
Note:

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#11 lore raw

Re: [LTP] [PATCH v4] lib: Use backoff polling to wait for loop device nodes

In reply to Wake Liu via ltp

Hi Wake,

I sent the Reviewed-by tag to the wrong email. @Li the agent review is correct and backoff polling is not working with old API. We need to find an another way for that.

As you can see the CI is failing everywhere in v4:
https://patchwork.kernel.org/project/ltp/patch/[email protected]/

Regards
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#12 lore raw

Re: [LTP] [PATCH v4] lib: Use backoff polling to wait for loop device nodes

Quoted 8 lines · Andrea Cervesato via ltp

Hi Wake,

I sent the Reviewed-by tag to the wrong email. @Li the agent review is correct and backoff polling is not working with old API. We need to find an another way for that.

As you can see the CI is failing everywhere in v4:
https://patchwork.kernel.org/project/ltp/patch/[email protected]/

Yes, a simple way to fix it is to declare the tst_multiply_timeout() in tst_common.h directly, then the lib can build successfully.

include/tst_common.h +2 -0
@@ -26,6 +26,8 @@
 #define LTP_ALIGN(x, a)    __LTP_ALIGN_MASK(x, (typeof(x))(a) - 1)
 #define __LTP_ALIGN_MASK(x, mask)  (((x) + (mask)) & ~(mask))
 
+unsigned int tst_multiply_timeout(unsigned int timeout);
+
 /**
  * TST_RETRY_FUNC() - Repeatedly retry a function with an increasing delay.
  * @FUNC - The function which will be retried
Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#13 lore raw

PATCH v5 1/1 lib: Use backoff polling to wait for loop device nodes

On systems where loop device node creation is asynchronous (such as Android containers or systems with slow udev startup), calling stat() or open() immediately after LOOP_CTL_GET_FREE can transiently fail because the device file (e.g. /dev/loopX) has not been fully populated in time.

Introduce an exponential-backoff retry loop in both tst_find_free_loopdev() and tst_attach_device() (starting at 1ms delay, doubling each try, capped at 100ms) to wait for the device node to be successfully populated.

This improves the robustness of loop device allocations on asynchronous virtualized environments while minimizing unnecessary delays on responsive systems.

---
v4 -> v5:
- Declare tst_multiply_timeout() in tst_common.h to fix old API build failures in CI.

v3 -> v4:
- Rebase onto the latest LTP HEAD.
- Remove self-referencing Link in commit message.

v2 -> v3:
- Reuse LTP native TST_RETRY_FN_EXP_BACKOFF() macro in tst_common.h to simplify code.

v1 -> v2:
- Strictly clamp backoff delay to 100ms (prevent overflow exceeding cap).
- Skip usleep on final loop iteration to avoid unnecessary idle delay.
- Reduce max attempts to 30 (stat) and 15 (open) to bound worst-case timeout.

 include/tst_common.h |  2 ++
 lib/tst_device.c     | 13 +++++++++----
 2 files changed, 11 insertions(+), 4 deletions(-)
include/tst_common.h +2 -0
index e1f7c7907..d09edbbbe 100644
@@ -26,6 +26,8 @@
 #define LTP_ALIGN(x, a)    __LTP_ALIGN_MASK(x, (typeof(x))(a) - 1)
 #define __LTP_ALIGN_MASK(x, mask)  (((x) + (mask)) & ~(mask))
 
+unsigned int tst_multiply_timeout(unsigned int timeout);
+
 /**
  * TST_RETRY_FUNC() - Repeatedly retry a function with an increasing delay.
  * @FUNC - The function which will be retried
lib/tst_device.c +9 -4
index 744173ffe..00304711b 100644
@@ -82,7 +82,7 @@ static int set_dev_path(char *dev, char *path, size_t path_len)
 
 int tst_find_free_loopdev(char *path, size_t path_len)
 {
-	int ctl_fd, dev_fd, rc, i;
+	int ctl_fd, dev_fd, rc, i, path_set;
 	struct loop_info loopinfo;
 	char buf[PATH_MAX];
 
@@ -93,8 +93,13 @@ int tst_find_free_loopdev(char *path, size_t path_len)
 		rc = ioctl(ctl_fd, LOOP_CTL_GET_FREE);
 		close(ctl_fd);
 		if (rc >= 0) {
-			if (path && set_dev_loop_path(rc, path, path_len))
-				tst_brkm(TBROK, NULL, "Could not stat loop device %i", rc);
+			if (path) {
+				path_set = TST_RETRY_FN_EXP_BACKOFF(
+					set_dev_loop_path(rc, path, path_len),
+					TST_RETVAL_EQ0, 1);
+				if (path_set)
+					tst_brkm(TBROK, NULL, "Could not stat loop device %i", rc);
+			}
 			tst_resm(TINFO, "Found free device %d '%s'",
 				rc, path ?: "");
 			return rc;
@@ -161,7 +166,7 @@ int tst_attach_device(const char *dev, const char *file)
 			 LO_NAME_SIZE);
 	}
 
-	dev_fd = open(dev, O_RDWR);
+	dev_fd = TST_RETRY_FN_EXP_BACKOFF(open(dev, O_RDWR), TST_RETVAL_GE0, 1);
 	if (dev_fd < 0) {
 		tst_resm(TWARN | TERRNO, "open('%s', O_RDWR) failed", dev);
 		return 1;
Signature · 5 lines
2.55.0.654.g21b8a5bc05-goog


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#14 lore raw

Re: [LTP] lib: Use backoff polling to wait for loop device nodes

Hi Wake,

Quoted 6 lines · Wake Liu

lib: Use backoff polling to wait for loop device nodes

Introduce an exponential-backoff retry loop in both tst_find_free_loopdev() and tst_attach_device() (starting at 1ms delay, doubling each try, capped at 100ms) to wait for the device node to be successfully populated.

Could the timing description be corrected, or the implementation changed to match it? TST_RETRY_FN_EXP_BACKOFF() starts tst_delay_ at 1 and passes it to usleep(), so the first delay is 1 microsecond. MAX_DELAY is converted from seconds to microseconds, so passing 1 retries for about one second before LTP timeout scaling, not 100 ms.

Verdict - Needs revision

---
Note:

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#15 lore raw

PATCH v6 1/1 lib: Use backoff polling to wait for loop device nodes

On systems where loop device node creation is asynchronous (such as Android containers or systems with slow udev startup), calling stat() or open() immediately after LOOP_CTL_GET_FREE can transiently fail because the device file (e.g. /dev/loopX) has not been fully populated in time.

Introduce an exponential-backoff retry loop in both tst_find_free_loopdev() and tst_attach_device() by reusing the LTP native TST_RETRY_FN_EXP_BACKOFF() macro (starting at 1us delay, doubling each try, with a base timeout limit of 1 second that scales automatically with LTP_TIMEOUT_MUL) to wait for the device node to be successfully populated.

This improves the robustness of loop device allocations on asynchronous virtualized environments while minimizing unnecessary delays on responsive systems.

---
v5 -> v6:
- Correct timing descriptions in commit message to match the actual behavior of TST_RETRY_FN_EXP_BACKOFF() macro (1us initial delay, 1s base timeout).

v4 -> v5:
- Declare tst_multiply_timeout() in tst_common.h to fix old API build failures in CI.

v3 -> v4:
- Rebase onto the latest LTP HEAD.
- Remove self-referencing Link in commit message.

v2 -> v3:
- Reuse LTP native TST_RETRY_FN_EXP_BACKOFF() macro in tst_common.h to simplify code.

v1 -> v2:
- Strictly clamp backoff delay to 100ms (prevent overflow exceeding cap).
- Skip usleep on final loop iteration to avoid unnecessary idle delay.
- Reduce max attempts to 30 (stat) and 15 (open) to bound worst-case timeout.

 include/tst_common.h |  2 ++
 lib/tst_device.c     | 13 +++++++++----
 2 files changed, 11 insertions(+), 4 deletions(-)
include/tst_common.h +2 -0
index e1f7c7907..d09edbbbe 100644
@@ -26,6 +26,8 @@
 #define LTP_ALIGN(x, a)    __LTP_ALIGN_MASK(x, (typeof(x))(a) - 1)
 #define __LTP_ALIGN_MASK(x, mask)  (((x) + (mask)) & ~(mask))
 
+unsigned int tst_multiply_timeout(unsigned int timeout);
+
 /**
  * TST_RETRY_FUNC() - Repeatedly retry a function with an increasing delay.
  * @FUNC - The function which will be retried
lib/tst_device.c +9 -4
index 744173ffe..00304711b 100644
@@ -82,7 +82,7 @@ static int set_dev_path(char *dev, char *path, size_t path_len)
 
 int tst_find_free_loopdev(char *path, size_t path_len)
 {
-	int ctl_fd, dev_fd, rc, i;
+	int ctl_fd, dev_fd, rc, i, path_set;
 	struct loop_info loopinfo;
 	char buf[PATH_MAX];
 
@@ -93,8 +93,13 @@ int tst_find_free_loopdev(char *path, size_t path_len)
 		rc = ioctl(ctl_fd, LOOP_CTL_GET_FREE);
 		close(ctl_fd);
 		if (rc >= 0) {
-			if (path && set_dev_loop_path(rc, path, path_len))
-				tst_brkm(TBROK, NULL, "Could not stat loop device %i", rc);
+			if (path) {
+				path_set = TST_RETRY_FN_EXP_BACKOFF(
+					set_dev_loop_path(rc, path, path_len),
+					TST_RETVAL_EQ0, 1);
+				if (path_set)
+					tst_brkm(TBROK, NULL, "Could not stat loop device %i", rc);
+			}
 			tst_resm(TINFO, "Found free device %d '%s'",
 				rc, path ?: "");
 			return rc;
@@ -161,7 +166,7 @@ int tst_attach_device(const char *dev, const char *file)
 			 LO_NAME_SIZE);
 	}
 
-	dev_fd = open(dev, O_RDWR);
+	dev_fd = TST_RETRY_FN_EXP_BACKOFF(open(dev, O_RDWR), TST_RETVAL_GE0, 1);
 	if (dev_fd < 0) {
 		tst_resm(TWARN | TERRNO, "open('%s', O_RDWR) failed", dev);
 		return 1;
Signature · 5 lines
2.55.0.654.g21b8a5bc05-goog


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#16 lore raw

Re: [LTP] lib: Use backoff polling to wait for loop device nodes

Hi Wake,

lib/tst_device.c int tst_find_free_loopdev(char *path, size_t path_len) · line 99

Quoted 5 lines · Wake Liu

lib: Use backoff polling to wait for loop device nodes

lib/tst_device.c +3 -0
+				path_set = TST_RETRY_FN_EXP_BACKOFF(
+					set_dev_loop_path(rc, path, path_len),
+					TST_RETVAL_EQ0, 1);

Could the backoff arithmetic be made overflow-safe before these two new uses?

LTP_TIMEOUT_MUL=3000 is accepted by parse_mul(), which makes tst_max_delay_ 3,000,000,000. The unsigned int tst_delay_ reaches 2,147,483,648, still compares less than tst_max_delay_, and then doubles to zero. If the device node does not appear, both new paths then retry forever instead of honoring the multiplied timeout.

Verdict - Needs revision

---
Note:

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#17 lore raw

Re: [LTP] lib: Use backoff polling to wait for loop device nodes

Hi Wake,

Quoted 5 lines · On Sat, Aug 08, 2026 at 01:32:46AM +0000, [email protected]…

Hi Wake,

Quoted 1 line · Wake Liu

lib: Use backoff polling to wait for loop device nodes

Quoted 8 lines

lib/tst_device.c int tst_find_free_loopdev(char *path, size_t path_len) · line 99

Quoted 3 lines
lib/tst_device.c +3 -0
+				path_set = TST_RETRY_FN_EXP_BACKOFF(
+					set_dev_loop_path(rc, path, path_len),
+					TST_RETVAL_EQ0, 1);

Could the backoff arithmetic be made overflow-safe before these two new uses?

LTP_TIMEOUT_MUL=3000 is accepted by parse_mul(), which makes

Theoretically, this is correct, but I don't think anyone will need a timeout as large as 3000 * 30s for one of LTP test. It would take nearly 25 hours to trigger, which is unrealistic.

IOW, your patch v6 is good enough. Sometimes AI agent is too strict.

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#18 lore raw

Re: [LTP] lib: Use backoff polling to wait for loop device nodes

Hi Li,

Thanks for the review.

I agree that LTP_TIMEOUT_MUL=3000 is an extreme case and unlikely to happen in practice.

However, since we already sent v7 which fixes this overflow cleanly (using 'unsigned long long' in the macro to prevent the wrap-around), and the AI reviewer has already passed it (Verdict - Reviewed), I think it's worth having the more robust version in v7.

Please take a look at v7.

Thanks,
Wake

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#19 lore raw

v7 cover Fix overflow and use backoff polling for loop devices

In reply to Wake Liu via ltp

This is v7 of the patch series to use backoff polling to wait for loop device nodes.

Since v6, we have split the changes into two patches and optimized the overflow fix:
1. Fix potential overflow in TST_RETRY_FN_EXP_BACKOFF() and tst_multiply_timeout() by using 'unsigned long long' for delay tracking variables.
2. Use backoff polling for loop device nodes (original patch logic).

This addresses the feedback from the LTP AI Reviewer regarding overflow when LTP_TIMEOUT_MUL is large.

Wake Liu (2):
lib: Fix overflow in backoff polling and timeout multiplication
lib: Use backoff polling to wait for loop device nodes

 include/tst_common.h |  4 +++-
 lib/tst_device.c     | 13 +++++++++----
 lib/tst_test.c       | 11 +++++++++--
 3 files changed, 21 insertions(+), 7 deletions(-)
Signature · 5 lines
2.55.0.654.g21b8a5bc05-goog


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#20 lore raw

PATCH v7 1/2 lib: Fix overflow in backoff polling and timeout multiplication

Prevent integer overflow in TST_RETRY_FN_EXP_BACKOFF() when the delay doubles. By using 'unsigned long long' for delay variables, we ensure the doubled delay is safely represented without wrapping to 0 (which causes an infinite loop). The loop will naturally terminate when the 64-bit delay exceeds 'tst_max_delay_' (capped at UINT_MAX).

Also fix potential overflow in tst_multiply_timeout() when the multiplied timeout exceeds UINT_MAX. Perform the calculation in double precision and cap the result at UINT_MAX.

---
 include/tst_common.h |  4 +++-
 lib/tst_test.c       | 11 +++++++++--
 2 files changed, 12 insertions(+), 3 deletions(-)
include/tst_common.h +3 -1
index e1f7c7907..058060fa4 100644
@@ -26,6 +26,8 @@
 #define LTP_ALIGN(x, a)    __LTP_ALIGN_MASK(x, (typeof(x))(a) - 1)
 #define __LTP_ALIGN_MASK(x, mask)  (((x) + (mask)) & ~(mask))
 
+unsigned int tst_multiply_timeout(unsigned int timeout);
+
 /**
  * TST_RETRY_FUNC() - Repeatedly retry a function with an increasing delay.
  * @FUNC - The function which will be retried
@@ -42,7 +44,7 @@
 	TST_RETRY_FN_EXP_BACKOFF(FUNC, ECHCK, 1)
 
 #define TST_RETRY_FN_EXP_BACKOFF(FUNC, ECHCK, MAX_DELAY)	\
-({	unsigned int tst_delay_, tst_max_delay_;			\
+({	unsigned long long tst_delay_, tst_max_delay_;			\
 	typeof(FUNC) tst_ret_;						\
 	tst_delay_ = 1;							\
 	tst_max_delay_ = tst_multiply_timeout(MAX_DELAY * 1000000);	\
lib/tst_test.c +9 -2
index 5c3607016..80463e7c6 100644
@@ -1855,10 +1855,17 @@ unsigned int tst_multiply_timeout(unsigned int timeout)
 	if (timeout < 1)
 		tst_brk(TBROK, "timeout must to be >= 1! (%d)", timeout);
 
+	double t = timeout;
+
 	if (tst_has_slow_kconfig())
-		timeout *= 4;
+		t *= 4;
+
+	t *= timeout_mul;
+
+	if (t > (double)UINT_MAX)
+		return UINT_MAX;
 
-	return timeout * timeout_mul;
+	return t;
 }
 
 static void set_overall_timeout(void)
Signature · 5 lines
2.55.0.654.g21b8a5bc05-goog


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#21 lore raw

Re: [LTP] lib: Fix overflow in backoff polling and timeout multiplication

Hi Wake,

Quoted 1 line · Wake Liu

lib: Fix overflow in backoff polling and timeout multiplication

Verdict - Reviewed

---
Note:

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#23 lore raw

Re: [LTP] [PATCH v7 1/2] lib: Fix overflow in backoff polling and timeout multiplication

In reply to Wake Liu via ltp

Hi Wake,

This patch is not necessary, the overflow is not reachable in practice.

MAX_DELAY is a compile-time constant <= 30 at all call sites, so 'MAX_DELAY * 1000000' never overflows int. And the backoff would need tst_delay_ to reach 2^31 (a single ~35 min usleep) to wrap, which the loop-device probing never approaches regardless of LTP_TIMEOUT_MUL.

So, I'd prefer to keep the change minimal and apply only v6.

Quoted 68 lines · Wake Liu via ltp

Prevent integer overflow in TST_RETRY_FN_EXP_BACKOFF() when the delay doubles. By using 'unsigned long long' for delay variables, we ensure the doubled delay is safely represented without wrapping to 0 (which causes an infinite loop). The loop will naturally terminate when the 64-bit delay exceeds 'tst_max_delay_' (capped at UINT_MAX).

Also fix potential overflow in tst_multiply_timeout() when the multiplied timeout exceeds UINT_MAX. Perform the calculation in double precision and cap the result at UINT_MAX.

---
 include/tst_common.h |  4 +++-
 lib/tst_test.c       | 11 +++++++++--
 2 files changed, 12 insertions(+), 3 deletions(-)
include/tst_common.h +3 -1
index e1f7c7907..058060fa4 100644
@@ -26,6 +26,8 @@
 #define LTP_ALIGN(x, a)    __LTP_ALIGN_MASK(x, (typeof(x))(a) - 1)
 #define __LTP_ALIGN_MASK(x, mask)  (((x) + (mask)) & ~(mask))
 
+unsigned int tst_multiply_timeout(unsigned int timeout);
+
 /**
  * TST_RETRY_FUNC() - Repeatedly retry a function with an increasing delay.
  * @FUNC - The function which will be retried
@@ -42,7 +44,7 @@
 	TST_RETRY_FN_EXP_BACKOFF(FUNC, ECHCK, 1)
 
 #define TST_RETRY_FN_EXP_BACKOFF(FUNC, ECHCK, MAX_DELAY)	\
-({	unsigned int tst_delay_, tst_max_delay_;			\
+({	unsigned long long tst_delay_, tst_max_delay_;			\
 	typeof(FUNC) tst_ret_;						\
 	tst_delay_ = 1;							\
 	tst_max_delay_ = tst_multiply_timeout(MAX_DELAY * 1000000);	\
lib/tst_test.c +9 -2
index 5c3607016..80463e7c6 100644
@@ -1855,10 +1855,17 @@ unsigned int tst_multiply_timeout(unsigned int timeout)
 	if (timeout < 1)
 		tst_brk(TBROK, "timeout must to be >= 1! (%d)", timeout);
 
+	double t = timeout;
+
 	if (tst_has_slow_kconfig())
-		timeout *= 4;
+		t *= 4;
+
+	t *= timeout_mul;
+
+	if (t > (double)UINT_MAX)
+		return UINT_MAX;
 
-	return timeout * timeout_mul;
+	return t;
 }
 
 static void set_overall_timeout(void)
Signature · 5 lines
2.55.0.654.g21b8a5bc05-goog


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#24 lore raw

PATCH v7 2/2 lib: Use backoff polling to wait for loop device nodes

In reply to Wake Liu via ltp

On systems where loop device node creation is asynchronous (such as Android containers or systems with slow udev startup), calling stat() or open() immediately after LOOP_CTL_GET_FREE can transiently fail because the device file (e.g. /dev/loopX) has not been fully populated in time.

Introduce an exponential-backoff retry loop in both tst_find_free_loopdev() and tst_attach_device() by reusing the LTP native TST_RETRY_FN_EXP_BACKOFF() macro (starting at 1us delay, doubling each try, with a base timeout limit of 1 second that scales automatically with LTP_TIMEOUT_MUL) to wait for the device node to be successfully populated.

This improves the robustness of loop device allocations on asynchronous virtualized environments while minimizing unnecessary delays on responsive systems.

---
 lib/tst_device.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
lib/tst_device.c +9 -4
index 744173ffe..00304711b 100644
@@ -82,7 +82,7 @@ static int set_dev_path(char *dev, char *path, size_t path_len)
 
 int tst_find_free_loopdev(char *path, size_t path_len)
 {
-	int ctl_fd, dev_fd, rc, i;
+	int ctl_fd, dev_fd, rc, i, path_set;
 	struct loop_info loopinfo;
 	char buf[PATH_MAX];
 
@@ -93,8 +93,13 @@ int tst_find_free_loopdev(char *path, size_t path_len)
 		rc = ioctl(ctl_fd, LOOP_CTL_GET_FREE);
 		close(ctl_fd);
 		if (rc >= 0) {
-			if (path && set_dev_loop_path(rc, path, path_len))
-				tst_brkm(TBROK, NULL, "Could not stat loop device %i", rc);
+			if (path) {
+				path_set = TST_RETRY_FN_EXP_BACKOFF(
+					set_dev_loop_path(rc, path, path_len),
+					TST_RETVAL_EQ0, 1);
+				if (path_set)
+					tst_brkm(TBROK, NULL, "Could not stat loop device %i", rc);
+			}
 			tst_resm(TINFO, "Found free device %d '%s'",
 				rc, path ?: "");
 			return rc;
@@ -161,7 +166,7 @@ int tst_attach_device(const char *dev, const char *file)
 			 LO_NAME_SIZE);
 	}
 
-	dev_fd = open(dev, O_RDWR);
+	dev_fd = TST_RETRY_FN_EXP_BACKOFF(open(dev, O_RDWR), TST_RETVAL_GE0, 1);
 	if (dev_fd < 0) {
 		tst_resm(TWARN | TERRNO, "open('%s', O_RDWR) failed", dev);
 		return 1;
Signature · 5 lines
2.55.0.654.g21b8a5bc05-goog


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#26 lore raw

Re: [LTP] [PATCH v6] lib: Use backoff polling to wait for loop device nodes

In reply to Wake Liu via ltp

Hi Wake, All,

I have merged v6 to keep things tidy. Thanks!

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#27 lore raw

Re: [LTP] [PATCH v6] lib: Use backoff polling to wait for loop device nodes

Got it, thanks!!!

Quoted 8 lines · Li Wang

Hi Wake, All,

I have merged v6 to keep things tidy. Thanks!

--
Regards,
Li Wang

Signature · 5 lines
Best Regards,
Wake Liu

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#28 lore raw

Re: [LTP] [PATCH v6] lib: Use backoff polling to wait for loop device nodes

In reply to Wake Liu via ltp

Hi,

d90d2004657b ("lib: Use backoff polling to wait for loop
device nodes")

This patch introduces a regression in few old API based tests which call tst_acquire_device() -> tst_find_free_loopdev()/tst_attach_device()):

- linkat02
- mknodat02
- rename11
- renameat01
- umount2_01

these fail with
tst_kconfig.c:90: TINFO: Parsing kernel config '/proc/config.gz'

linkat02    1  TBROK  :  tst_sig.c:232: unexpected signal SIGCHLD(17) received (pid = 122504).
linkat02    2  TBROK  :  Remaining cases broken

which comes from def_handler(int sig) in lib/tst_sig.c

The old API treats SIGCHLD as fatal, which is now being generated while reading the kconfig file. So we need to address this until we convert these tests to new API, maybe by intentionally avoiding TST_RETRY_FN_EXP_BACKOFF()/tst_multiply_timeout() for now.

Regards,
Avinesh

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#29 lore raw

Re: [LTP] [PATCH v6] lib: Use backoff polling to wait for loop device nodes

Hi all,

Quoted 18 lines

d90d2004657b ("lib: Use backoff polling to wait for loop
device nodes")

This patch introduces a regression in few old API based tests which call tst_acquire_device() -> tst_find_free_loopdev()/tst_attach_device()):

- linkat02
- mknodat02
- rename11
- renameat01
- umount2_01

these fail with
tst_kconfig.c:90: TINFO: Parsing kernel config '/proc/config.gz'
linkat02 1 TBROK : tst_sig.c:232: unexpected signal SIGCHLD(17)
received (pid = 122504).
linkat02 2 TBROK : Remaining cases broken

Quoted 6 lines

which comes from def_handler(int sig) in lib/tst_sig.c

The old API treats SIGCHLD as fatal, which is now being generated while reading the kconfig file. So we need to address this until we convert these tests to new API, maybe by intentionally avoiding TST_RETRY_FN_EXP_BACKOFF()/tst_multiply_timeout() for now.

Avinesh, thanks for the report.

I vote to revert d90d2004657b until we convert the tests above. Anybody objects?

If you reply ack, I can revert it later tonight/tomorrow morning (UTC timezone) and add your ABT to the commit.

Kind regards,
Petr

Quoted 2 lines

Regards,
Avinesh

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp