* [LTP] [PATCH] tst_detach_device: Clear leftover partitions
@ 2025-12-12 10:40 Martin Doucha
2025-12-12 14:39 ` Petr Vorel
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Martin Doucha @ 2025-12-12 10:40 UTC (permalink / raw)
To: ltp
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.
Signed-off-by: Martin Doucha <mdoucha@suse.cz>
---
lib/tst_device.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 78 insertions(+), 1 deletion(-)
diff --git a/lib/tst_device.c b/lib/tst_device.c
index 6ae914720..85f5f8cac 100644
--- a/lib/tst_device.c
+++ b/lib/tst_device.c
@@ -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;
--
2.51.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [LTP] [PATCH] tst_detach_device: Clear leftover partitions
2025-12-12 10:40 [LTP] [PATCH] tst_detach_device: Clear leftover partitions Martin Doucha
@ 2025-12-12 14:39 ` Petr Vorel
2025-12-16 12:13 ` Andrea Cervesato via ltp
2025-12-18 13:11 ` Andrea Cervesato via ltp
2 siblings, 0 replies; 6+ messages in thread
From: Petr Vorel @ 2025-12-12 14:39 UTC (permalink / raw)
To: Martin Doucha; +Cc: ltp
Hi Martin,
> 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
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [LTP] [PATCH] tst_detach_device: Clear leftover partitions
2025-12-12 10:40 [LTP] [PATCH] tst_detach_device: Clear leftover partitions Martin Doucha
2025-12-12 14:39 ` Petr Vorel
@ 2025-12-16 12:13 ` Andrea Cervesato via ltp
2025-12-16 12:16 ` Martin Doucha
2025-12-18 13:11 ` Andrea Cervesato via ltp
2 siblings, 1 reply; 6+ messages in thread
From: Andrea Cervesato via ltp @ 2025-12-16 12:13 UTC (permalink / raw)
To: Martin Doucha, ltp
Hi!
On Fri Dec 12, 2025 at 11:40 AM CET, Martin Doucha wrote:
> 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.
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
andrea.cervesato@suse.com
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [LTP] [PATCH] tst_detach_device: Clear leftover partitions
2025-12-16 12:13 ` Andrea Cervesato via ltp
@ 2025-12-16 12:16 ` Martin Doucha
2025-12-16 12:19 ` Andrea Cervesato via ltp
0 siblings, 1 reply; 6+ messages in thread
From: Martin Doucha @ 2025-12-16 12:16 UTC (permalink / raw)
To: Andrea Cervesato, ltp
On 12/16/25 13:13, Andrea Cervesato wrote:
> 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.
--
Martin Doucha mdoucha@suse.cz
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [LTP] [PATCH] tst_detach_device: Clear leftover partitions
2025-12-16 12:16 ` Martin Doucha
@ 2025-12-16 12:19 ` Andrea Cervesato via ltp
0 siblings, 0 replies; 6+ messages in thread
From: Andrea Cervesato via ltp @ 2025-12-16 12:19 UTC (permalink / raw)
To: Martin Doucha, ltp
On Tue Dec 16, 2025 at 1:16 PM CET, Martin Doucha wrote:
> On 12/16/25 13:13, Andrea Cervesato wrote:
> > 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.
Reviewed-by: Andrea Cervesato <andrea.cervesato@suse.com>
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
andrea.cervesato@suse.com
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [LTP] [PATCH] tst_detach_device: Clear leftover partitions
2025-12-12 10:40 [LTP] [PATCH] tst_detach_device: Clear leftover partitions Martin Doucha
2025-12-12 14:39 ` Petr Vorel
2025-12-16 12:13 ` Andrea Cervesato via ltp
@ 2025-12-18 13:11 ` Andrea Cervesato via ltp
2 siblings, 0 replies; 6+ messages in thread
From: Andrea Cervesato via ltp @ 2025-12-18 13:11 UTC (permalink / raw)
To: Martin Doucha, ltp
Hi!
Merged, Thanks.
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
andrea.cervesato@suse.com
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2025-12-18 13:12 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-12 10:40 [LTP] [PATCH] tst_detach_device: Clear leftover partitions Martin Doucha
2025-12-12 14:39 ` Petr Vorel
2025-12-16 12:13 ` Andrea Cervesato via ltp
2025-12-16 12:16 ` Martin Doucha
2025-12-16 12:19 ` Andrea Cervesato via ltp
2025-12-18 13:11 ` Andrea Cervesato via ltp
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox