diff mbox series

[V4,blktests] loop: Detect a race condition between loop detach and open

Message ID 20240605161815.34923-1-gulam.mohamed@oracle.com (mailing list archive)
State New, archived
Headers show
Series [V4,blktests] loop: Detect a race condition between loop detach and open | expand

Commit Message

Gulam Mohamed June 5, 2024, 4:18 p.m. UTC
When one process opens a loop device partition and another process detaches
it, there will be a race condition due to which stale loop partitions are
created causing IO errors. This test will detect the race.

Signed-off-by: Gulam Mohamed <gulam.mohamed@oracle.com>
---
v4<-v3:
1. Resolved formatting issues
2. Using long options for commands instead of short options

 tests/loop/010     | 75 ++++++++++++++++++++++++++++++++++++++++++++++
 tests/loop/010.out |  2 ++
 2 files changed, 77 insertions(+)
 create mode 100755 tests/loop/010
 create mode 100644 tests/loop/010.out

Comments

Chaitanya Kulkarni June 5, 2024, 6:17 p.m. UTC | #1
On 6/5/2024 9:18 AM, Gulam Mohamed wrote:
> When one process opens a loop device partition and another process detaches
> it, there will be a race condition due to which stale loop partitions are
> created causing IO errors. This test will detect the race.
> 
> Signed-off-by: Gulam Mohamed<gulam.mohamed@oracle.com>


Looks good.

Reviewed-by: Chaitanya Kulkarni <kch@nvidia.com>

-ck
Shin'ichiro Kawasaki June 6, 2024, 6:02 a.m. UTC | #2
On Jun 05, 2024 / 16:18, Gulam Mohamed wrote:
> When one process opens a loop device partition and another process detaches
> it, there will be a race condition due to which stale loop partitions are
> created causing IO errors. This test will detect the race.
> 
> Signed-off-by: Gulam Mohamed <gulam.mohamed@oracle.com>
> ---
> v4<-v3:
> 1. Resolved formatting issues
> 2. Using long options for commands instead of short options

Thank you for the v4 patch. Looks good to me. Before applying it, I will wait
for some more days in case anyone has other comments.
Gulam Mohamed June 6, 2024, 9:11 a.m. UTC | #3
Hi Shinichiro,

> -----Original Message-----
> From: Shinichiro Kawasaki <shinichiro.kawasaki@wdc.com>
> Sent: Thursday, June 6, 2024 11:33 AM
> To: Gulam Mohamed <gulam.mohamed@oracle.com>
> Cc: linux-block@vger.kernel.org; chaitanyak@nvidia.com
> Subject: Re: [PATCH V4 blktests] loop: Detect a race condition between loop
> detach and open
> 
> On Jun 05, 2024 / 16:18, Gulam Mohamed wrote:
> > When one process opens a loop device partition and another process
> > detaches it, there will be a race condition due to which stale loop
> > partitions are created causing IO errors. This test will detect the race.
> >
> > Signed-off-by: Gulam Mohamed <gulam.mohamed@oracle.com>
> > ---
> > v4<-v3:
> > 1. Resolved formatting issues
> > 2. Using long options for commands instead of short options
> 
> Thank you for the v4 patch. Looks good to me. Before applying it, I will wait
> for some more days in case anyone has other comments.

Thank you very much for reviewing all these versions.
Shin'ichiro Kawasaki June 10, 2024, 7:47 a.m. UTC | #4
On Jun 06, 2024 / 09:11, Gulam Mohamed wrote:
> Hi Shinichiro,
> 
> > -----Original Message-----
> > From: Shinichiro Kawasaki <shinichiro.kawasaki@wdc.com>
> > Sent: Thursday, June 6, 2024 11:33 AM
> > To: Gulam Mohamed <gulam.mohamed@oracle.com>
> > Cc: linux-block@vger.kernel.org; chaitanyak@nvidia.com
> > Subject: Re: [PATCH V4 blktests] loop: Detect a race condition between loop
> > detach and open
> > 
> > On Jun 05, 2024 / 16:18, Gulam Mohamed wrote:
> > > When one process opens a loop device partition and another process
> > > detaches it, there will be a race condition due to which stale loop
> > > partitions are created causing IO errors. This test will detect the race.
> > >
> > > Signed-off-by: Gulam Mohamed <gulam.mohamed@oracle.com>
> > > ---
> > > v4<-v3:
> > > 1. Resolved formatting issues
> > > 2. Using long options for commands instead of short options
> > 
> > Thank you for the v4 patch. Looks good to me. Before applying it, I will wait
> > for some more days in case anyone has other comments.
> 
> Thank you very much for reviewing all these versions.

FYI, I've applied this patch. Please note that I folded in a minor change.
When I took another look in the patch, I noticed that $TMPDIR was referred out
of the test() function. Actually, $TMPDIR works only within test(), so I moved
it into test(). Anyway, thank you for this work.
diff mbox series

Patch

diff --git a/tests/loop/010 b/tests/loop/010
new file mode 100755
index 000000000000..f5d1bf1f9c18
--- /dev/null
+++ b/tests/loop/010
@@ -0,0 +1,75 @@ 
+#!/bin/bash
+# SPDX-License-Identifier: GPL-3.0+
+# Copyright (C) 2024, Oracle and/or its affiliates.
+#
+# Test to detect a race between loop detach and loop open which creates
+# stale loop partitions when one process opens the loop partition and
+# another process detaches the loop device.
+#
+. tests/loop/rc
+DESCRIPTION="check stale loop partition"
+TIMED=1
+
+requires() {
+	_have_program parted
+	_have_program mkfs.xfs
+}
+
+image_file="$TMPDIR/loopImg"
+
+create_loop() {
+	while true
+	do
+		loop_device="$(losetup --partscan --find --show "${image_file}")"
+		blkid /dev/loop0p1 >& /dev/null
+	done
+}
+
+detach_loop() {
+	while true
+	do
+		if [ -e /dev/loop0 ]; then
+			losetup --detach /dev/loop0 >& /dev/null
+		fi
+	done
+}
+
+test() {
+	echo "Running ${TEST_NAME}"
+	local loop_device
+	local create_pid
+	local detach_pid
+
+	truncate --size 1G "${image_file}"
+	parted --align none --script "${image_file}" mklabel gpt
+	loop_device="$(losetup --partscan --find --show "${image_file}")"
+	parted --align none --script "${loop_device}" mkpart primary 64s 109051s
+
+	udevadm settle
+
+	if [ ! -e "${loop_device}" ]; then
+		return 1
+	fi
+
+	mkfs.xfs --force "${loop_device}p1" >& /dev/null
+	losetup --detach "${loop_device}" >&  /dev/null
+
+	create_loop &
+	create_pid=$!
+	detach_loop &
+	detach_pid=$!
+
+	sleep "${TIMEOUT:-90}"
+	{
+		kill -9 $create_pid
+		kill -9 $detach_pid
+		wait
+		sleep 1
+	} 2>/dev/null
+
+	losetup --detach-all >& /dev/null
+	if _dmesg_since_test_start | grep --quiet "partition scan of loop0 failed (rc=-16)"; then
+		echo "Fail"
+	fi
+	echo "Test complete"
+}
diff --git a/tests/loop/010.out b/tests/loop/010.out
new file mode 100644
index 000000000000..64a6aee00b8a
--- /dev/null
+++ b/tests/loop/010.out
@@ -0,0 +1,2 @@ 
+Running loop/010
+Test complete