Skip to contentSkip to conversation

tst_detach_device: Clear leftover partitions

PATCH Applied Martin Doucha 3 participants 6 messages Dec 12, 2025 – Dec 18, 2025 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tst_detach_device: Clear leftover partitions

Some kernels have a race condition during loop device release which results in partitions being left over on unattached device. The partitions then cause mkfs.vfat failures in later tests.

Check for loop device partitions after detaching it and clear them if necessary.

---
 lib/tst_device.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 78 insertions(+), 1 deletion(-)
lib/tst_device.c +78 -1
index 6ae914720..85f5f8cac 100644
@@ -239,7 +239,7 @@ uint64_t tst_get_device_size(const char *dev_path)
 	return size/1024/1024;
 }
 
-int tst_detach_device_by_fd(const char *dev, int *dev_fd)
+static int detach_loop_fd(const char *dev, int *dev_fd)
 {
 	int ret, i, retval = 1;
 
@@ -275,6 +275,83 @@ exit:
 	return retval;
 }
 
+static int find_loop_device_partition(const char *dev, char *part_path,
+	unsigned int path_size)
+{
+	int dev_num = -1;
+	unsigned int i;
+
+	snprintf(part_path, path_size, "%sp1", dev);
+
+	if (!access(part_path, F_OK))
+		return 1;
+
+	/* Parse loop device number */
+	for (i = 0; i < ARRAY_SIZE(dev_loop_variants); i++) {
+		if (sscanf(dev, dev_loop_variants[i], &dev_num) == 1)
+			break;
+
+		dev_num = -1;
+	}
+
+	if (dev_num < 0) {
+		tst_resm(TWARN, "Cannot parse %s device number", dev);
+		return 0;
+	}
+
+	snprintf(part_path, path_size, "/sys/block/loop%d/loop%dp1", dev_num,
+		dev_num);
+
+	if (!access(part_path, F_OK))
+		return 1;
+
+	/* The loop device has no leftover partitions */
+	return 0;
+}
+
+static int clear_loop_device_partitions(const char *dev)
+{
+	char part_path[PATH_MAX];
+	struct loop_info loopinfo = {};
+	int dev_fd;
+
+	if (!find_loop_device_partition(dev, part_path, PATH_MAX))
+		return 0;
+
+	tst_resm(TWARN, "Detached device %s has leftover partitions", dev);
+	tst_fill_file(DEV_FILE, 0, 1024 * 1024, 1);
+	tst_attach_device(dev, DEV_FILE);
+	dev_fd = open(dev, O_RDWR);
+
+	if (dev_fd < 0) {
+		tst_resm(TWARN | TERRNO,
+			"Cannot clear leftover partitions on %s", dev);
+		/* Do not detach device to prevent infinite recursion */
+		return 1;
+	}
+
+	loopinfo.lo_flags = LO_FLAGS_PARTSCAN;
+	ioctl(dev_fd, LOOP_SET_STATUS, &loopinfo);
+
+	if (!access(part_path, F_OK)) {
+		tst_resm(TWARN, "Cannot clear leftover partitions on %s", dev);
+		detach_loop_fd(dev, &dev_fd);
+		return 1;
+	}
+
+	return detach_loop_fd(dev, &dev_fd);
+}
+
+int tst_detach_device_by_fd(const char *dev, int *dev_fd)
+{
+	int ret = detach_loop_fd(dev, dev_fd);
+
+	if (!ret)
+		ret = clear_loop_device_partitions(dev);
+
+	return ret;
+}
+
 int tst_detach_device(const char *dev)
 {
 	int dev_fd, ret;
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi Martin,

Quoted 6 lines

Some kernels have a race condition during loop device release which results in partitions being left over on unattached device. The partitions then cause mkfs.vfat failures in later tests.

Check for loop device partitions after detaching it and clear them if necessary.

Nice work, thanks!

Looping ioctl_loop01.c and ioctl_ficlone02.c I reproduce the problem after 171 iterations:

ioctl_loop01.c:75: TPASS: access /sys/block/loop0/loop0p1 succeeds tst_device.c:321: TWARN: Detached device /dev/loop0 has leftover partitions

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw
In reply to Martin Doucha

Hi!

Quoted 6 lines · Martin Doucha

Some kernels have a race condition during loop device release which results in partitions being left over on unattached device. The partitions then cause mkfs.vfat failures in later tests.

Check for loop device partitions after detaching it and clear them if necessary.

In general the code looks good, but the sentence "Some kernels have a race condition during loop device release" sounds like a kernel bug that we are working around with this feature. I know probably this is not the case, but if I read the sentence this is what I guess.

Signature · 7 lines
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw
Quoted 4 lines · Andrea Cervesato

In general the code looks good, but the sentence "Some kernels have a race condition during loop device release" sounds like a kernel bug that we are working around with this feature. I know probably this is not the case, but if I read the sentence this is what I guess.

Yes, this is a kernel bug and yes, we are working around it. But the workaround is not there to hide it, it's there to flag the bug in the appropriate test which triggered it and prevent other unrelated tests from failing. Think of it as extra thorough post-test environment cleanup.

Signature · 10 lines
Martin Doucha   [email protected]
SW Quality Engineer
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by Andrea Cervesato
#4 lore raw
Quoted 10 lines · Martin Doucha
Quoted 4 lines · Andrea Cervesato

In general the code looks good, but the sentence "Some kernels have a race condition during loop device release" sounds like a kernel bug that we are working around with this feature. I know probably this is not the case, but if I read the sentence this is what I guess.

Yes, this is a kernel bug and yes, we are working around it. But the workaround is not there to hide it, it's there to flag the bug in the appropriate test which triggered it and prevent other unrelated tests from failing. Think of it as extra thorough post-test environment cleanup.

Ok thanks, now it's more clear. It would be nice to add this comment to the commit message.

Signature · 7 lines
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]


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