All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH v18 0/3] fsconfig04: Check FSCONFIG_SET_PATH
@ 2026-11-02  2:22 Wei Gao via ltp
  2026-11-02  2:22 ` [LTP] [PATCH v18 1/3] lib: Add tst_attach_loopdev() Wei Gao via ltp
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Wei Gao via ltp @ 2026-11-02  2:22 UTC (permalink / raw)
  To: ltp

v17->v18:
- lib: Add tst_attach_loopdev() to find an unused loop device and attach a
  backing file with exponential backoff retry on EBUSY, preventing race
  conditions between lookup and attachment.
- newlib_tests: Add unit test verifying tst_attach_loopdev().
- fsconfig04: Update create_and_attach_loopdev() to use tst_attach_loopdev().

Wei Gao (3):
  lib: Add tst_attach_loopdev()
  newlib_tests: Add tst_attach_loopdev test
  fsconfig04: Check FSCONFIG_SET_PATH

 include/tst_device.h                          |  13 ++
 lib/newlib_tests/.gitignore                   |   1 +
 lib/newlib_tests/runtest.sh                   |   1 +
 lib/newlib_tests/tst_attach_loopdev.c         | 133 ++++++++++++++
 lib/tst_device.c                              |  87 ++++++++-
 runtest/syscalls                              |   1 +
 testcases/kernel/syscalls/fsconfig/.gitignore |   1 +
 .../kernel/syscalls/fsconfig/fsconfig04.c     | 166 ++++++++++++++++++
 8 files changed, 395 insertions(+), 8 deletions(-)
 create mode 100644 lib/newlib_tests/tst_attach_loopdev.c
 create mode 100644 testcases/kernel/syscalls/fsconfig/fsconfig04.c

-- 
2.55.0


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

^ permalink raw reply	[flat|nested] 7+ messages in thread
* [LTP] [PATCH v19 1/3] lib: Add tst_attach_loopdev()
@ 2026-11-16  8:56 Wei Gao via ltp
  2026-11-16 12:16 ` [LTP] " linuxtestproject.agent
  0 siblings, 1 reply; 7+ messages in thread
From: Wei Gao via ltp @ 2026-11-16  8:56 UTC (permalink / raw)
  To: ltp

Calling tst_find_free_loopdev() followed by tst_attach_device() has
a race window where another process can claim the loop device before
LOOP_SET_FD is issued, causing attachment to fail with EBUSY.

Introduce tst_attach_loopdev() which finds an unused loop device and
attaches a backing file to it, retrying the lookup with exponential
backoff if the candidate device is claimed concurrently (EBUSY).
The function returns a newly allocated string containing the device
path on success, or NULL on failure. Also introduce the
SAFE_ATTACH_LOOPDEV() helper macro.

Update tst_acquire_loop_device() to use tst_attach_loopdev().

Suggested-by: Cyril Hrubis <chrubis@suse.cz>
Signed-off-by: Wei Gao <wegao@suse.com>
---
 include/tst_device.h | 26 ++++++++++++++
 lib/tst_device.c     | 82 ++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 101 insertions(+), 7 deletions(-)

diff --git a/include/tst_device.h b/include/tst_device.h
index 0989e4501..4e2e14b89 100644
--- a/include/tst_device.h
+++ b/include/tst_device.h
@@ -110,6 +110,32 @@ int tst_find_free_loopdev(char *path, size_t path_len);
  */
 int tst_attach_device(const char *dev_path, const char *file_path);
 
+/**
+ * tst_attach_loopdev() - Find a free loop device and attach a file to it.
+ * @file_path: Path to a file (e.g. disk.img).
+ *
+ * Finds an unused loop device and attaches @file_path to it, automatically
+ * retrying the lookup if the chosen device is claimed concurrently (EBUSY).
+ *
+ * Return: Newly allocated string containing the device path on success,
+ *         or NULL on failure. Caller is responsible for free()-ing the string.
+ */
+char *tst_attach_loopdev(const char *file_path);
+
+/**
+ * safe_attach_loopdev() - Safe wrapper for tst_attach_loopdev().
+ * @file: Source file where called.
+ * @lineno: Line number where called.
+ * @file_path: Path to a file (e.g. disk.img).
+ *
+ * Return: Newly allocated string containing the device path. Calls tst_brk()
+ *         on failure. Caller is responsible for free()-ing the string.
+ */
+char *safe_attach_loopdev(const char *file, const int lineno, const char *file_path);
+
+#define SAFE_ATTACH_LOOPDEV(file_path) \
+	safe_attach_loopdev(__FILE__, __LINE__, (file_path))
+
 /**
  * tst_get_device_size() - Get device size in megabytes.
  * @dev_path: Device path.
diff --git a/lib/tst_device.c b/lib/tst_device.c
index bf77c9ded..c751e576f 100644
--- a/lib/tst_device.c
+++ b/lib/tst_device.c
@@ -156,7 +156,7 @@ int tst_find_free_loopdev(char *path, size_t path_len)
 	return -1;
 }
 
-int tst_attach_device(const char *dev, const char *file)
+static int attach_device(const char *dev, const char *file, int warn_on_ebusy)
 {
 	int dev_fd, file_fd;
 	struct loop_info loopinfo;
@@ -168,7 +168,8 @@ int tst_attach_device(const char *dev, const char *file)
 
 	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);
+		if (warn_on_ebusy || errno != EBUSY)
+			tst_resm(TWARN | TERRNO, "open('%s', O_RDWR) failed", dev);
 		return 1;
 	}
 
@@ -180,10 +181,16 @@ int tst_attach_device(const char *dev, const char *file)
 	}
 
 	if (ioctl(dev_fd, LOOP_SET_FD, file_fd) < 0) {
+		int err = errno;
+
 		close(dev_fd);
 		close(file_fd);
-		tst_resm(TWARN | TERRNO, "ioctl(%s, LOOP_SET_FD, %s) failed",
-			 dev, file);
+		errno = err;
+
+		if (warn_on_ebusy || errno != EBUSY) {
+			tst_resm(TWARN | TERRNO, "ioctl(%s, LOOP_SET_FD, %s) failed",
+				 dev, file);
+		}
 		return 1;
 	}
 
@@ -207,6 +214,64 @@ int tst_attach_device(const char *dev, const char *file)
 	return 0;
 }
 
+int tst_attach_device(const char *dev, const char *file)
+{
+	return attach_device(dev, file, 1);
+}
+
+static int attach_loopdev(char *dev, size_t dev_len, const char *file)
+{
+	if (tst_find_free_loopdev(dev, dev_len) == -1)
+		return -1;
+
+	if (attach_device(dev, file, 0) == 0)
+		return 0;
+
+	if (errno == EBUSY)
+		return 1;
+
+	return -1;
+}
+
+char *tst_attach_loopdev(const char *file)
+{
+	char dev[PATH_MAX];
+	unsigned int delay = 1;
+	unsigned int max_delay = tst_multiply_timeout(1000000);
+	int ret;
+
+	for (;;) {
+		ret = attach_loopdev(dev, sizeof(dev), file);
+		if (ret <= 0)
+			break;
+
+		if (delay < max_delay) {
+			usleep(delay);
+			delay *= 2;
+		} else {
+			ret = -1;
+			break;
+		}
+	}
+
+	if (ret == 0)
+		return strdup(dev);
+
+	return NULL;
+}
+
+char *safe_attach_loopdev(const char *file, const int lineno, const char *file_path)
+{
+	char *dev = tst_attach_loopdev(file_path);
+
+	if (!dev) {
+		tst_brkm_(file, lineno, TBROK, NULL,
+			  "Failed to attach %s to loop device", file_path);
+	}
+
+	return dev;
+}
+
 uint64_t tst_get_device_size(const char *dev_path)
 {
 	int fd;
@@ -386,17 +451,20 @@ int tst_dev_sync(int fd)
 const char *tst_acquire_loop_device(unsigned int size, const char *filename)
 {
 	unsigned int acq_dev_size = size ? size : DEV_SIZE_MB;
+	char *dev;
 
 	if (tst_prealloc_file(filename, 1024 * 1024, acq_dev_size)) {
 		tst_resm(TWARN | TERRNO, "Failed to create %s", filename);
 		return NULL;
 	}
 
-	if (tst_find_free_loopdev(dev_path, sizeof(dev_path)) == -1)
+	dev = tst_attach_loopdev(filename);
+	if (!dev)
 		return NULL;
 
-	if (tst_attach_device(dev_path, filename))
-		return NULL;
+	strncpy(dev_path, dev, sizeof(dev_path) - 1);
+	dev_path[sizeof(dev_path) - 1] = '\0';
+	free(dev);
 
 	return dev_path;
 }
-- 
2.55.0


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

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

end of thread, other threads:[~2026-11-16 12:16 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-02  2:22 [LTP] [PATCH v18 0/3] fsconfig04: Check FSCONFIG_SET_PATH Wei Gao via ltp
2026-11-02  2:22 ` [LTP] [PATCH v18 1/3] lib: Add tst_attach_loopdev() Wei Gao via ltp
2026-11-02  8:25   ` [LTP] " linuxtestproject.agent
2026-11-12 13:05   ` [LTP] [PATCH v18 1/3] " Cyril Hrubis
2026-11-02  2:22 ` [LTP] [PATCH v18 2/3] newlib_tests: Add tst_attach_loopdev test Wei Gao via ltp
2026-11-02  2:22 ` [LTP] [PATCH v18 3/3] fsconfig04: Check FSCONFIG_SET_PATH Wei Gao via ltp
  -- strict thread matches above, loose matches on Subject: below --
2026-11-16  8:56 [LTP] [PATCH v19 1/3] lib: Add tst_attach_loopdev() Wei Gao via ltp
2026-11-16 12:16 ` [LTP] " linuxtestproject.agent

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.