Bug 1566390

Summary: [RHEL-7.5] losetup can not detach all loop devices
Product: Red Hat Enterprise Linux 7 Reporter: xhe <xhe>
Component: util-linuxAssignee: Karel Zak <kzak>
Status: CLOSED ERRATA QA Contact: Radka Brychtova <rskvaril>
Severity: high Docs Contact:
Priority: unspecified    
Version: 7.5CC: rskvaril
Target Milestone: rc   
Target Release: ---   
Hardware: x86_64   
OS: Linux   
Whiteboard:
Fixed In Version: util-linux-2.23.2-56.el7 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2018-10-30 10:52:39 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description xhe@redhat.com 2018-04-12 08:39:46 UTC
Description of problem:
[RHEL-7.5] losetup can not detach all loop devices

Version-Release number of selected component (if applicable):
util-linux-2.23.2-52.el7.x86_64

How reproducible:
often

Steps to Reproduce:
1. losetup /dev/loopx -f /tmp/tfile_loopx
2. losetup -d
3. losetup -a

Actual results:
# losetup --all
/dev/loop0: [64768]:1001699 (/tmp/tfile_loop0)
/dev/loop2: [64768]:1001699 (/tmp/tfile_loop0)
/dev/loop8: [64768]:1001681 (/tmp/tfile_loop1)
# losetup -D
# losetup --all
/dev/loop0: [64768]:1001699 (/tmp/tfile_loop0)
/dev/loop2: [64768]:1001699 (/tmp/tfile_loop0)

# strace losetup -d /dev/loop2
execve("/usr/sbin/losetup", ["losetup", "-d", "/dev/loop2"], [/* 39 vars */]) = 0
brk(NULL)                               = 0x1f88000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f72200dc000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=44171, ...}) = 0
mmap(NULL, 44171, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f72200d1000
close(3)                                = 0
open("/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\340$\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=2151832, ...}) = 0
mmap(NULL, 3981792, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f721faef000
mprotect(0x7f721fcb2000, 2093056, PROT_NONE) = 0
mmap(0x7f721feb1000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c2000) = 0x7f721feb1000
mmap(0x7f721feb7000, 16864, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f721feb7000
close(3)                                = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f72200d0000
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f72200ce000
arch_prctl(ARCH_SET_FS, 0x7f72200ce740) = 0
mprotect(0x7f721feb1000, 16384, PROT_READ) = 0
mprotect(0x611000, 4096, PROT_READ)     = 0
mprotect(0x7f72200dd000, 4096, PROT_READ) = 0
munmap(0x7f72200d1000, 44171)           = 0
brk(NULL)                               = 0x1f88000
brk(0x1fa9000)                          = 0x1fa9000
brk(NULL)                               = 0x1fa9000
open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=106070960, ...}) = 0
mmap(NULL, 106070960, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f72195c6000
close(3)                                = 0
stat("/sys/block", {st_mode=S_IFDIR|0755, st_size=0, ...}) = 0
uname({sysname="Linux", nodename="storageqe-52.rhts.eng.pek2.redhat.com", ...}) = 0
stat("/dev/loop-control", {st_mode=S_IFCHR|0660, st_rdev=makedev(10, 237), ...}) = 0
open("/dev/loop2", O_RDONLY|O_CLOEXEC)  = 3
ioctl(3, LOOP_CLR_FD)                   = 0
close(3)                                = 0
close(1)                                = 0
close(2)                                = 0
exit_group(0)                           = ?
+++ exited with 0 +++

# losetup --all
/dev/loop0: [64768]:1001699 (/tmp/tfile_loop0)
/dev/loop2: [64768]:1001699 (/tmp/tfile_loop0)


Expected results:


Additional info:

Comment 2 Karel Zak 2018-04-13 09:43:39 UTC
This is nothing unusual, if there is some reader of the loop device than kernel does not destroy the device, but mark by LO_FLAGS_AUTOCLEAR. The device will be 
detached later when reference counting will be zero.

As you can see from the strace than 

  ioctl(3, LOOP_CLR_FD)                   = 0

kernel returns success, in this case losetup cannot do anything more.

man losetup (upstream):

       -d, --detach loopdev...
              Detach  the  file  or device associated with the specified loop device(s). Note that since Linux v3.7 kernel uses "lazy device destruction".  The detach operation does not
              return EBUSY error anymore if device is actively used by system, but it is marked by autoclear flag and destroyed later.


Maybe  we can add this note to RHEL7.6 too.

Comment 8 errata-xmlrpc 2018-10-30 10:52:39 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHBA-2018:3178