[LTP] [PATCH v7 2/2] lib: Use backoff polling to wait for loop device nodes
Wake Liu
wakel@google.com
Mon Aug 10 04:58:17 CEST 2026
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.
Signed-off-by: Wake Liu <wakel@google.com>
---
lib/tst_device.c | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/lib/tst_device.c b/lib/tst_device.c
index 744173ffe..00304711b 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,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;
--
2.55.0.654.g21b8a5bc05-goog
More information about the ltp
mailing list