Bug 1324551

Summary: Hotplug of memory/rng device fails after unplugging device of the same type that is not last
Product: Red Hat Enterprise Linux 7 Reporter: Peter Krempa <pkrempa>
Component: libvirtAssignee: Peter Krempa <pkrempa>
Status: CLOSED ERRATA QA Contact: Virtualization Bugs <virt-bugs>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 7.2CC: dyuan, lhuang, mzhan, pzhang, rbalakri
Target Milestone: rc   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: libvirt-1.3.4-1.el7 Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2016-11-03 18:41:11 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:

Description Peter Krempa 2016-04-06 15:31:27 UTC
Description of problem:
After unplugging a memory/rng device other than the last one further attempts of hotplug fail.


How reproducible:
100%

Steps to Reproduce:
1. Create a guest with multiple memory/rng devices
2. unplug first one
3. try to hotplug same device type

Actual results:
error: internal error: unable to execute QEMU command 'object-add': attempt to add duplicate property 'memdimm3' to object (type 'container')


Expected results:
Success

Additional info:
The bug is in the alias assignment code that takes the position of the device rather than checking if it's available.

Comment 1 Peter Krempa 2016-04-07 08:18:41 UTC
Fixed upstream:

commit be6e92f541ab27019de530f1f08ec9b2912765a9
Author: Peter Krempa <pkrempa>
Date:   Wed Apr 6 17:32:12 2016 +0200

    qemu: alias: Fix calculation of memory device aliases
    
    For device hotplug, the new alias ID needs to be checked in the list
    rather than using the count of devices. Unplugging a device that is not
    last in the array will make further hotplug impossible due to alias
    collision.
    
    Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1324551

commit bd19b4b25beb22335571b80d6a4a32f7012b87e1
Author: Peter Krempa <pkrempa>
Date:   Wed Apr 6 17:32:12 2016 +0200

    qemu: alias: Fix calculation of RNG device aliases
    
    For device hotplug, the new alias ID needs to be checked in the list
    rather than using the count of devices. Unplugging a device that is not
    last in the array will make further hotplug impossible due to alias
    collision.
    
    Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1324551

Comment 3 Pei Zhang 2016-08-25 08:21:41 UTC
Verify version :
libvirt-2.0.0-6.el7.x86_64
qemu-kvm-rhev-2.6.0-22.el7.x86_64

Verify steps :

1. hot-unplug memory device, hot-plug same type deice.

1.1 start a guest with multiple memory deivces like following :
#virsh dumpxml vm1 | grep memory -A 9
...... 
      <memory model='dimm'>
      <target>
        <size unit='KiB'>131072</size>
        <node>1</node>
      </target>
      <alias name='dimm0'/>
      <address type='dimm' slot='0' base='0x440000000'/>
    </memory>
    <memory model='dimm'>
      <target>
        <size unit='KiB'>131072</size>
        <node>2</node>
      </target>
      <alias name='dimm1'/>
      <address type='dimm' slot='1' base='0x448000000'/>
    </memory>

1.2 prepare a memory device like following(as same as the first one in guest).
# cat de-mem.xml 
  <memory model='dimm'>
      <target>
        <size unit='KiB'>131072</size>
        <node>1</node>
      </target>
      <alias name='dimm0'/>
      <address type='dimm' slot='0' base='0x440000000'/>
    </memory>

1.3 detach memory device successfully. 
# virsh detach-device vm1 mem.xml 
Device detached successfully

#virsh dumpxml vm1 | grep memory -A 8
......
  <memory model='dimm'>
      <target>
        <size unit='KiB'>131072</size>
        <node>2</node>
      </target>
      <alias name='dimm1'/>
      <address type='dimm' slot='1' base='0x448000000'/>
    </memory>
......

1.4 re-plug memory device again, check the alias name
it won't use the count of device, it will use a new one
in the list.

# virsh attach-device vm1 mem.xml 
Device attached successfully

# virsh dumpxml vm1 | grep memory -A 7
......
  <memory model='dimm'>
      <target>
        <size unit='KiB'>131072</size>
        <node>2</node>
      </target>
      <alias name='dimm1'/>
      <address type='dimm' slot='1' base='0x448000000'/>
    </memory>
    <memory model='dimm'>
      <target>
        <size unit='KiB'>131072</size>
        <node>1</node>
      </target>
      <alias name='dimm2'/>
      <address type='dimm' slot='0' base='0x440000000'/>
    </memory>

# virsh attach-device vm1 mem.xml 
Device attached successfully

# virsh dumpxml vm1 | grep memory -A 7
......
  <memory model='dimm'>
      <target>
        <size unit='KiB'>131072</size>
        <node>2</node>
      </target>
      <alias name='dimm1'/>
      <address type='dimm' slot='1' base='0x448000000'/>
    </memory>
    <memory model='dimm'>
      <target>
        <size unit='KiB'>131072</size>
        <node>1</node>
      </target>
      <alias name='dimm2'/>
      <address type='dimm' slot='0' base='0x440000000'/>
    </memory>
     <memory model='dimm'>
      <target>
        <size unit='KiB'>131072</size>
        <node>2</node>
      </target>
      <alias name='dimm3'/>
      <address type='dimm' slot='3' base='0x458000000'/>
    </memory>

2. hot-unplug rng device, replug

2.1 start a guest with multiple rng devices like following: 
# virsh dumpxml vm1 | grep rng -A 8
    <rng model='virtio'>
      <backend model='random'>/dev/random</backend>
      <alias name='rng0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>
    </rng>
    <rng model='virtio'>
      <backend model='random'>/dev/random</backend>
      <alias name='rng1'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x0a' function='0x0'/>
    </rng>
    <rng model='virtio'>
      <backend model='random'>/dev/random</backend>
      <alias name='rng2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x0b' function='0x0'/>
    </rng>

2.2 prepare a xml like following (as same as the first rng device in guest)
# cat rng.xml 
 <rng model='virtio'>
      <backend model='random'>/dev/random</backend>
      <alias name='rng0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>
    </rng>

2.3 detach device successfully

# virsh detach-device vm1 rng.xml 
Device detached successfully

check guest xml
# virsh dumpxml vm1 | grep rng -A 8
    <rng model='virtio'>
      <backend model='random'>/dev/random</backend>
      <alias name='rng1'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x0a' function='0x0'/>
    </rng>
    <rng model='virtio'>
      <backend model='random'>/dev/random</backend>
      <alias name='rng2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x0b' function='0x0'/>
    </rng>

2.4 re-plug the device 

# virsh attach-device vm1 rng.xml 
Device attached successfully

# virsh dumpxml vm1 | grep rng -A 8
......
    <rng model='virtio'>
      <backend model='random'>/dev/random</backend>
      <alias name='rng2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x0b' function='0x0'/>
    </rng>
    <rng model='virtio'>
      <backend model='random'>/dev/random</backend>
      <alias name='rng3'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>
    </rng>

# virsh attach-device vm1 rng.xml 
Device attached successfully

# virsh dumpxml vm1 | grep rng -A 8
    <rng model='virtio'>
      <backend model='random'>/dev/random</backend>
      <alias name='rng2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x0b' function='0x0'/>
    </rng>
    <rng model='virtio'>
      <backend model='random'>/dev/random</backend>
      <alias name='rng3'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>
    </rng>
    <rng model='virtio'>
      <backend model='random'>/dev/random</backend>
      <alias name='rng4'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x0a' function='0x0'/>
    </rng>

Now, for the memory/rng device the alias name can be checked in the list, not using the count of devices. 
Move to verified.

Comment 5 errata-xmlrpc 2016-11-03 18:41:11 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://rhn.redhat.com/errata/RHSA-2016-2577.html