Linux Test Project
 help / color / mirror / Atom feed
* [LTP] [PATCH] lib: Use exponential-backoff polling to wait for loop device nodes
@ 2026-08-03 15:27 Wake Liu via ltp
  2026-08-03 16:28 ` [LTP] " linuxtestproject.agent
  2026-08-04  1:59 ` [LTP] [PATCH v2] lib: Use backoff " Wake Liu via ltp
  0 siblings, 2 replies; 17+ messages in thread
From: Wake Liu via ltp @ 2026-08-03 15:27 UTC (permalink / raw)
  To: ltp; +Cc: wakel

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.

Signed-off-by: Wake Liu <wakel@google.com>
---
 lib/tst_device.c | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)

diff --git a/lib/tst_device.c b/lib/tst_device.c
index 744173ffef..6e3c6fef21 100644
--- a/lib/tst_device.c
+++ b/lib/tst_device.c
@@ -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;
-- 
2.55.0.508.g3f0d502094-goog


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

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

end of thread, other threads:[~2026-08-08  1:33 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-03 15:27 [LTP] [PATCH] lib: Use exponential-backoff polling to wait for loop device nodes Wake Liu via ltp
2026-08-03 16:28 ` [LTP] " linuxtestproject.agent
2026-08-04  1:59 ` [LTP] [PATCH v2] lib: Use backoff " Wake Liu via ltp
2026-08-04  3:58   ` Li Wang
2026-08-04 15:24     ` [LTP] [PATCH v3] " Wake Liu via ltp
2026-08-05  2:24       ` Li Wang
2026-08-05  2:54         ` [LTP] [PATCH v4] " Wake Liu via ltp
2026-08-05  4:09           ` [LTP] " linuxtestproject.agent
2026-08-05  6:53             ` Li Wang
2026-08-05  6:35           ` [LTP] [PATCH v4] " Li Wang
2026-08-05  6:52           ` Andrea Cervesato via ltp
2026-08-05  6:55           ` Andrea Cervesato via ltp
2026-08-05  7:10             ` Li Wang
2026-08-07 10:04               ` [LTP] [PATCH v5] " Wake Liu via ltp
2026-08-07 11:05                 ` [LTP] " linuxtestproject.agent
2026-08-07 23:49                   ` [LTP] [PATCH v6] " Wake Liu via ltp
2026-08-08  1:32                     ` [LTP] " linuxtestproject.agent

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox