From: Wei Gao via ltp <ltp@lists.linux.it>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v19 2/3] newlib_tests: Add tst_attach_loopdev test
Date: Mon, 16 Nov 2026 08:56:05 +0000 [thread overview]
Message-ID: <20261116085612.20171-3-wegao@suse.com> (raw)
In-Reply-To: <20261116085612.20171-1-wegao@suse.com>
Add a unit test in lib/newlib_tests/ to verify the newly added
tst_attach_loopdev() function.
The test covers:
- Concurrent allocation of multiple loop devices without collisions.
- Parallel worker stress testing to exercise the exponential backoff
retry loop upon transient EBUSY conditions.
Suggested-by: Cyril Hrubis <chrubis@suse.cz>
Signed-off-by: Wei Gao <wegao@suse.com>
---
lib/newlib_tests/.gitignore | 1 +
lib/newlib_tests/runtest.sh | 1 +
lib/newlib_tests/tst_attach_loopdev.c | 145 ++++++++++++++++++++++++++
3 files changed, 147 insertions(+)
create mode 100644 lib/newlib_tests/tst_attach_loopdev.c
diff --git a/lib/newlib_tests/.gitignore b/lib/newlib_tests/.gitignore
index 3a6e1643c..2d4b3d43f 100644
--- a/lib/newlib_tests/.gitignore
+++ b/lib/newlib_tests/.gitignore
@@ -11,6 +11,7 @@ test11
test13
test14
test15
+tst_attach_loopdev
tst_capability01
tst_capability02
tst_cgroup01
diff --git a/lib/newlib_tests/runtest.sh b/lib/newlib_tests/runtest.sh
index 71808ef8b..18b64c07a 100755
--- a/lib/newlib_tests/runtest.sh
+++ b/lib/newlib_tests/runtest.sh
@@ -16,6 +16,7 @@ test_zero_hugepage.sh
test0[579]
test1[59]
test2[04]
+tst_attach_loopdev
tst_bool_expr
tst_capability02
tst_checkpoint
diff --git a/lib/newlib_tests/tst_attach_loopdev.c b/lib/newlib_tests/tst_attach_loopdev.c
new file mode 100644
index 000000000..37bbcc6b9
--- /dev/null
+++ b/lib/newlib_tests/tst_attach_loopdev.c
@@ -0,0 +1,145 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2026 SUSE LLC <wegao@suse.com>
+ */
+
+/*\
+ * Unit test for tst_attach_loopdev().
+ *
+ * Verifies that:
+ * - Multiple backing files can be attached simultaneously to distinct
+ * loop devices without collision.
+ * - Concurrent workers attaching and detaching loop devices in parallel
+ * succeed without unhandled EBUSY race conditions.
+ */
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "tst_test.h"
+
+#define BACKING_FILE1 "test_loop1.img"
+#define BACKING_FILE2 "test_loop2.img"
+
+#define DEV_SIZE_MB 1
+#define NUM_WORKERS 4
+#define WORKER_LOOPS 5
+
+static char *dev1;
+static char *dev2;
+
+static void cleanup(void)
+{
+ if (dev1) {
+ tst_detach_device(dev1);
+ free(dev1);
+ dev1 = NULL;
+ }
+
+ if (dev2) {
+ tst_detach_device(dev2);
+ free(dev2);
+ dev2 = NULL;
+ }
+}
+
+static void setup(void)
+{
+ if (tst_prealloc_file(BACKING_FILE1, 1024 * 1024, DEV_SIZE_MB))
+ tst_brk(TBROK, "Failed to create %s", BACKING_FILE1);
+
+ if (tst_prealloc_file(BACKING_FILE2, 1024 * 1024, DEV_SIZE_MB))
+ tst_brk(TBROK, "Failed to create %s", BACKING_FILE2);
+}
+
+static void test_multiple_devices(void)
+{
+ dev1 = tst_attach_loopdev(BACKING_FILE1);
+ if (!dev1) {
+ tst_res(TFAIL, "Failed to attach %s to dev1", BACKING_FILE1);
+ return;
+ }
+
+ dev2 = tst_attach_loopdev(BACKING_FILE2);
+ if (!dev2) {
+ tst_res(TFAIL, "Failed to attach %s to dev2", BACKING_FILE2);
+ goto detach1;
+ }
+
+ if (!strcmp(dev1, dev2))
+ tst_res(TFAIL, "dev1 and dev2 assigned identical device: '%s'", dev1);
+ else
+ tst_res(TPASS, "Assigned distinct loop devices: '%s' and '%s'", dev1, dev2);
+
+ if (tst_detach_device(dev2))
+ tst_res(TFAIL, "Failed to detach dev2 '%s'", dev2);
+ else
+ tst_res(TPASS, "Detached dev2 '%s' successfully", dev2);
+
+ free(dev2);
+ dev2 = NULL;
+
+detach1:
+ if (tst_detach_device(dev1))
+ tst_res(TFAIL, "Failed to detach dev1 '%s'", dev1);
+ else
+ tst_res(TPASS, "Detached dev1 '%s' successfully", dev1);
+
+ free(dev1);
+ dev1 = NULL;
+}
+
+static void test_concurrent_contention(void)
+{
+ pid_t pids[NUM_WORKERS];
+ int i;
+
+ for (i = 0; i < NUM_WORKERS; i++) {
+ pids[i] = SAFE_FORK();
+ if (!pids[i]) {
+ char file[32];
+ char *dev;
+ int j;
+
+ snprintf(file, sizeof(file), "worker_%d.img", i);
+ if (tst_prealloc_file(file, 1024 * 1024, DEV_SIZE_MB))
+ tst_brk(TBROK, "Worker %d failed to prealloc %s", i, file);
+
+ for (j = 0; j < WORKER_LOOPS; j++) {
+ dev = SAFE_ATTACH_LOOPDEV(file);
+
+ if (tst_detach_device(dev)) {
+ free(dev);
+ tst_brk(TBROK, "Worker %d loop %d failed detach", i, j);
+ }
+
+ free(dev);
+ }
+
+ exit(0);
+ }
+ }
+
+ for (i = 0; i < NUM_WORKERS; i++)
+ SAFE_WAITPID(pids[i], NULL, 0);
+
+ tst_res(TPASS, "All %d workers attached and detached loop devices concurrently",
+ NUM_WORKERS);
+}
+
+static void run(void)
+{
+ test_multiple_devices();
+ test_concurrent_contention();
+}
+
+static struct tst_test test = {
+ .needs_root = 1,
+ .needs_tmpdir = 1,
+ .forks_child = 1,
+ .setup = setup,
+ .cleanup = cleanup,
+ .test_all = run,
+};
--
2.55.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next prev parent reply other threads:[~2026-11-16 8:57 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-16 8:56 [LTP] [PATCH v19 0/3] fsconfig04: Check FSCONFIG_SET_PATH Wei Gao via ltp
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
2026-11-16 8:56 ` Wei Gao via ltp [this message]
2026-11-16 8:56 ` [LTP] [PATCH v19 3/3] fsconfig04: Check FSCONFIG_SET_PATH Wei Gao via ltp
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261116085612.20171-3-wegao@suse.com \
--to=ltp@lists.linux.it \
--cc=wegao@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.