public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Li Wang via ltp <ltp@lists.linux.it>
To: ltp@lists.linux.it, rafael.tinoco@linaro.org, chrubis@suse.cz
Subject: [LTP] [PATCH] tst_umount: Retry open() after umount to handle delayed device release
Date: Wed,  6 Aug 2025 15:44:05 +0800	[thread overview]
Message-ID: <20250806074405.14890-1-liwang@redhat.com> (raw)
In-Reply-To: <20190102131913.GA26215@rei.lan>

Currently, tst_umount() in lib/tst_device.c tries to repeatedly umount()
a mount point, with a retry loop if it gets EBUSY. However, after umount()
reports success, devices (especially loop devices) can still be held open
by another process or kernel, delaying their actual release. This can lead
to race conditions when the next operation tries to reuse the device like
mkfs.ext3 error:

  ==== setxattr01 ====
  command: setxattr01
  tst_test.c:1953: TINFO: LTP version: 20250530
  ...
  tst_supported_fs_types.c:48: TINFO: mkfs is not needed for tmpfs
  tst_test.c:1888: TINFO: === Testing on ext2 ===
  tst_test.c:1217: TINFO: Formatting /dev/loop0 with ext2 opts='' extra opts=''
  mke2fs 1.47.1 (20-May-2024)
  tst_test.c:1229: TINFO: Mounting /dev/loop0 to /tmp/LTP_setd24dAf/mntpoint fstyp=ext2 flags=0
  ...
  setxattr01.c:174: TPASS: setxattr(2) failed: ERANGE (34)
  setxattr01.c:174: TPASS: setxattr(2) failed: EFAULT (14)
  tst_test.c:1888: TINFO: === Testing on ext3 ===
  tst_test.c:1217: TINFO: Formatting /dev/loop0 with ext3 opts='' extra opts='' mke2fs 1.47.1 (20-May-2024)

  /dev/loop0 is apparently in use by the system; will not make a filesystem here!
  tst_test.c:1217: TBROK: mkfs.ext3 failed with exit code 1

  ==== close_range01 ====
  command: close_range01
  tst_test.c:1953: TINFO: LTP version: 20250530
  ...
  tst_test.c:1888: TINFO: === Testing on ext2 ===
  tst_test.c:1217: TINFO: Formatting /dev/loop0 with ext2 opts='' extra opts=''
  mke2fs 1.47.1 (20-May-2024)
  tst_test.c:1229: TINFO: Mounting /dev/loop0 to /tmp/LTP_cloXeXI39/mnt fstyp=ext2 flags=0
  ...
  close_range01.c:188: TPASS: No kernel taints
  tst_test.c:1888: TINFO: === Testing on ext3 ===
  tst_test.c:1217: TINFO: Formatting /dev/loop0 with ext3 opts='' extra opts='' mke2fs 1.47.1 (20-May-2024)

  /dev/loop0 is apparently in use by the system; will not make a filesystem here!
  tst_test.c:1217: TBROK: mkfs.ext3 failed with exit code 1

This patch improves robustness by, after a successful umount(), attempting to
open() the device in O_RDWR mode and retrying if open() fails with EBUSY,
similar to how umount() is retried. This ensures that tst_umount() only returns
success after the device is truly available for reuse, reducing race conditions
in rapid test cycles.

Signed-off-by: Li Wang <liwang@redhat.com>
---
 lib/tst_device.c | 27 +++++++++++++++++++++++++--
 1 file changed, 25 insertions(+), 2 deletions(-)

diff --git a/lib/tst_device.c b/lib/tst_device.c
index 6d1abf065..7f0a4f8a4 100644
--- a/lib/tst_device.c
+++ b/lib/tst_device.c
@@ -397,14 +397,37 @@ int tst_clear_device(const char *dev)
 
 int tst_umount(const char *path)
 {
-	int err, ret, i;
+	int err, ret, i, j;
 
 	for (i = 0; i < 50; i++) {
 		ret = umount(path);
 		err = errno;
 
-		if (!ret)
+		if (!ret) {
+			/* After successful umount, check device open busy */
+			for (j = 0; j < 50; j++) {
+				int fd = open(path, O_RDWR);
+				if (fd >= 0) {
+					close(fd);
+					break;
+				}
+				if (errno == EBUSY) {
+					tst_resm(TINFO, "open('%s') got EBUSY after umount, retry %d...",
+						path, j+1);
+					usleep(100000);
+					continue;
+				}
+				break;
+			}
+			if (j == 50) {
+				tst_resm(TWARN, "Device '%s' is still busy after %d open retries",
+					path, 10);
+				errno = EBUSY;
+				return -1;
+			}
+
 			return 0;
+		}
 
 		if (err != EBUSY) {
 			tst_resm(TWARN, "umount('%s') failed with %s",
-- 
2.50.1


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

  reply	other threads:[~2025-08-06  7:44 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-19 16:33 [LTP] lib: tst_device.c - /dev/loop0 in use Rafael David Tinoco
2019-01-02 13:19 ` Cyril Hrubis
2025-08-06  7:44   ` Li Wang via ltp [this message]
2025-08-06  9:27     ` [LTP] [PATCH] tst_umount: Retry open() after umount to handle delayed device release Cyril Hrubis
2025-08-11 12:42       ` Li Wang via ltp
2025-08-11 14:20         ` Cyril Hrubis
2025-08-12  3:43           ` Li Wang via ltp
2025-08-12 11:17             ` Cyril Hrubis

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=20250806074405.14890-1-liwang@redhat.com \
    --to=ltp@lists.linux.it \
    --cc=chrubis@suse.cz \
    --cc=liwang@redhat.com \
    --cc=rafael.tinoco@linaro.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox