Bug 1870072

Summary: Failed to detach virtio-scsi controller which is unused
Product: Red Hat Enterprise Linux Advanced Virtualization Reporter: gaojianan <jgao>
Component: libvirtAssignee: Peter Krempa <pkrempa>
Status: CLOSED ERRATA QA Contact: Han Han <hhan>
Severity: medium Docs Contact:
Priority: unspecified    
Version: 8.0CC: ddepaula, jdenemar, lmen, meili, pkrempa, virt-maint, xuzhang, yalzhang
Target Milestone: rcKeywords: Triaged
Target Release: 8.3   
Hardware: x86_64   
OS: Linux   
Whiteboard:
Fixed In Version: libvirt-6.10.0-1.el8 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2021-05-25 06:43:34 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: 6.10.0
Embargoed:

Description gaojianan 2020-08-19 10:09:33 UTC
Description of problem:
Failed to detach virtio-scsi controller which is unused

Version-Release number of selected component (if applicable):
libvirt-6.6.0-2.virtcov.el8.x86_64
qemu-kvm-5.0.0-2.module+el8.3.0+7379+0505d6ca.x86_64

How reproducible:
100%

Steps to Reproduce:
1.Attach a virtio-scsi controller to VM with alias
# cat controller.xml
    <controller type='scsi' index='0' model='virtio-scsi'>
      <driver iothread='1'/>
      <alias name='ua-9874229d-d867-4932-ad28-279e9a777f25'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>
    </controller>
2.Check if any device uses the controller
# virsh dumpxml test|grep 'address.*drive' -B5 
      <driver name='qemu' type='qcow2'/>
      <source file='/var/lib/libvirt/images/RHEL-8.3-x86_64-latest.qcow2' index='1'/>
      <backingStore/>
      <target dev='sda' bus='sata'/>
      <alias name='sata0-0-0'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
---->>> Only used a sata controller

3.Try to detach the controller
# virsh detach-device test controller.xml 
error: Failed to detach device from controller.xml
error: operation failed: device cannot be detached: device is busy


Actual results:
As step3,detach the controller failed

Expected results:
Can detach the virtio-scsi controller successfully in step3

Additional info:
libvirt log:
2020-08-19 09:36:11.060+0000: 398437: debug : virDomainDetachDevice:8265 : dom=0x7f8690008380, (VM: name=test, uuid=4f4d401c-422e-41c9-9f72-795a8ca52b73), xml=  <controller type='scsi' index='0' model='virtio-scsi'>
      <alias name='ua-9874229a-d867-4932-ad28-279e9a777f25'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>
    </controller>

2020-08-19 09:36:11.060+0000: 398437: debug : virAccessManagerCheckDomain:239 : manager=0x562777d04df0(name=stack) driver=QEMU domain=0x7f867036d6a0 perm=2
2020-08-19 09:36:11.060+0000: 398437: debug : virAccessManagerCheckDomain:239 : manager=0x562777d04e50(name=none) driver=QEMU domain=0x7f867036d6a0 perm=2
2020-08-19 09:36:11.060+0000: 398437: debug : qemuDomainObjBeginJobInternal:840 : Starting job: job=modify agentJob=none asyncJob=none (vm=0x562777d1f6a0 name=test, current job=none agentJob=none async=none)
2020-08-19 09:36:11.060+0000: 398437: debug : qemuDomainObjBeginJobInternal:887 : Started job: modify (async=none vm=0x562777d1f6a0 name=test)
2020-08-19 09:36:11.061+0000: 398437: error : qemuDomainDetachPrepController:5326 : operation failed: device cannot be detached: device is busy

Comment 2 Peter Krempa 2020-11-16 15:25:55 UTC
Fixed upstream:

commit 6a1bb797a7f90de131e2f5ccf4592c9324f875b0
Author: Peter Krempa <pkrempa>
Date:   Fri Nov 13 14:16:45 2020 +0100

    qemuDomainControllerIsBusy: Fully populate switch statement
    
    Typecast the controller type variable to the appropriate type and add
    the missing controller types for future extension.
    
    Note that we currently allow only unplug of
    VIR_DOMAIN_CONTROLLER_TYPE_SCSI thus the other controller types which
    are not implemented return false now.
    
    Signed-off-by: Peter Krempa <pkrempa>
    Reviewed-by: Ján Tomko <jtomko>

commit 279ba2d1cceceb70fa2463da7075bfe7cb6e43a3
Author: Peter Krempa <pkrempa>
Date:   Fri Nov 13 14:15:19 2020 +0100

    qemuDomainDiskControllerIsBusy: Optimize checking for SCSI hostdevs
    
    Iterate through hostdevs only when the controller type is
    VIR_DOMAIN_CONTROLLER_TYPE_SCSI.
    
    Signed-off-by: Peter Krempa <pkrempa>
    Reviewed-by: Ján Tomko <jtomko>

commit 022f4d431b7fffc5caa28b9872a061360410e0b2
Author: Peter Krempa <pkrempa>
Date:   Fri Nov 13 14:07:40 2020 +0100

    qemuDomainDiskControllerIsBusy: Fix logic of matching disk bus to controller type
    
    The tests which match the disk bus to the controller type were backwards
    in this function. This meant that any disk bus type (such as
    VIR_DOMAIN_DISK_BUS_SATA) would not skip the controller index comparison
    even if the removed controller was of a different type.
    
    Switch the internals to a switch statement with selects the controller
    type in the first place and a proper type so that new controller types
    are added in the future.
    
    Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1870072
    Signed-off-by: Peter Krempa <pkrempa>
    Reviewed-by: Ján Tomko <jtomko>

commit a6d5a5712f68ee2f528b7f8b99feeec2c243ddf0
Author: Peter Krempa <pkrempa>
Date:   Fri Nov 13 13:53:57 2020 +0100

    qemuDomain(Disk)ControllerIsBusy: Fix function header format
    
    Signed-off-by: Peter Krempa <pkrempa>
    Reviewed-by: Ján Tomko <jtomko>

Comment 3 Han Han 2020-12-08 03:57:57 UTC
Test on qemu-kvm-5.2.0-0.module+el8.4.0+8855+a9e237a9.x86_64 libvirt-6.10.0-1.module+el8.4.0+8898+a84e86e1.x86_64:
1. Prepare a running VM with following: sata disk with bus number 0 and scsi controller with index 0, no scsi device attached to scsi controller:
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/tmp/sata'/>
      <backingStore/>
      <target dev='sda' bus='sata'/>
      <alias name='sata0-0-0'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
    <controller type='sata' index='0'>
      <alias name='ide'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x1f' function='0x2'/>
    </controller>
    <controller type='scsi' index='0' model='virtio-scsi'>
      <address type='pci' domain='0x0000' bus='0x03' slot='0x00' function='0x0'/>
    </controller>

2. Try to detach the scsi controller:
➜  ~ cat /tmp/scsi-controller.xml 
    <controller type='scsi' index='0' model='virtio-scsi'>
      <address type='pci' domain='0x0000' bus='0x03' slot='0x00' function='0x0'/>
    </controller>
➜  ~ virsh detach-device vm1 /tmp/scsi-controller.xml
Device detached successfully

➜  ~ virsh dumpxml vm1 |grep scsi

3. Then attach&detach the scsi controller:
➜  ~ virsh dumpxml vm1 |grep scsi
➜  ~ virsh attach-device vm1 /tmp/scsi-controller.xml                                                                                        
Device attached successfully

➜  ~ virsh dumpxml vm1 |grep scsi
    <controller type='scsi' index='0' model='virtio-scsi'>
      <alias name='scsi0'/>
➜  ~ virsh detach-device vm1 /tmp/scsi-controller.xml
Device detached successfully

➜  ~ virsh dumpxml vm1 |grep scsi 

It works as expected.

Comment 9 Han Han 2021-01-06 02:34:25 UTC
Test as comment3 on libvirt-6.10.0-1.module+el8.4.0+8898+a84e86e1.x86_64 qemu-kvm-5.2.0-2.module+el8.4.0+9186+ec44380f.x86_64. Pass

Comment 11 errata-xmlrpc 2021-05-25 06:43:34 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 (virt:av bug fix and enhancement update), 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-2021:2098