Bug 743671

Summary: USB device can be reassigned to another VM without error
Product: Red Hat Enterprise Linux 6 Reporter: Miroslav Vadkerti <mvadkert>
Component: libvirtAssignee: Michal Privoznik <mprivozn>
Status: CLOSED ERRATA QA Contact: Virtualization Bugs <virt-bugs>
Severity: high Docs Contact:
Priority: high    
Version: 6.2CC: acathrow, ajia, dallan, juzhang, laine, mzhan, rwu, smueller, syeghiay, tdavis, veillard, weizhan, yupzhang, zhpeng
Target Milestone: rc   
Target Release: ---   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: libvirt-0.9.10-21.el6 Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2012-06-20 06:32:04 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 826592    
Attachments:
Description Flags
start 2 guest with assign the same usb
none
second guest can start on second time none

Description Miroslav Vadkerti 2011-10-05 17:20:18 UTC
Description of problem:
Currently I am able to assign one USB device to two separate virtual machines. While the second assignment overwrites the owner and selinux label of the usb device after the first assignement

My testing scenario is this:

1. Have a USB device on the host:
Bus 006 Device 002: ID 03f0:1027 Hewlett-Packard Virtual keyboard and mouse

2. I create two separate xmls describing the virtual machine [1]

3. I check the permissions/SELinux label of the USB device:
# ls -Z /dev/bus/usb/006/
crw-rw-r--. root root system_u:object_r:usb_device_t:s0 001
crw-rw-r--. root root system_u:object_r:usb_device_t:s0 002

4. I create the first VM and check the USB device:
# virsh create guest1.xml
Domain RHEL6-1 created from guest1.xml

# ls -Z /dev/bus/usb/006/
crw-rw-r--. root root system_u:object_r:usb_device_t:s0 001
crw-rw-r--. qemu qemu system_u:object_r:svirt_image_t:s0:c741,c999 002

5. Now I create the second VM and check the permissions again:

# virsh create guest2.xml
Domain RHEL6-2 created from guest2.xml

# ls -Z /dev/bus/usb/006/
crw-rw-r--. root root system_u:object_r:usb_device_t:s0 001
crw-rw-r--. qemu qemu system_u:object_r:svirt_image_t:s0:c484,c564 002

Version-Release number of selected component (if applicable):
qemu-kvm-0.12.1.2-2.195.el6.x86_64
libvirt-0.9.4-14.el6.x86_64
kernel-2.6.32-203.el6.x86_64

How reproducible:
100%

Steps to Reproduce:
see description
  
Actual results:
libvirt doesn't prevent double assignment

Expected results:
libvirt prevents double assignment

Additional info:

Comment 1 Alex Jia 2011-10-28 08:11:25 UTC
I can reproduce this issue on rhel6 beta(2.6.32-193.el6.x86_64) with libvirt-0.9.4-19.el6.x86_64 and qemu-kvm-0.12.1.2-2.200.el6.x86_64:

# lsusb | grep Kingston
Bus 001 Device 004: ID 0951:1625 Kingston Technology DataTraveler 101 I

# ll /dev/bus/usb/001/ -Z
crw-rw-r--. root root system_u:object_r:usb_device_t:s0 001
crw-rw-r--. root root system_u:object_r:usb_device_t:s0 004

# virsh edit guest1 and guest2, and add the following <hostdev...> element block into guest XML configuration:

    <hostdev mode='subsystem' type='usb' managed='no'>
      <source>
        <vendor id='0x0951'/>
        <product id='0x1625'/>
      </source>
    </hostdev>

# virsh start guest1
Domain guest1 started

# ll /dev/bus/usb/001/ -Z
crw-rw-r--. root root system_u:object_r:usb_device_t:s0 001
crw-rw-r--. qemu qemu system_u:object_r:svirt_image_t:s0:c213,c954 004

# virsh start guest2
Domain guest2 started

# ll /dev/bus/usb/001/ -Z
crw-rw-r--. root root system_u:object_r:usb_device_t:s0 001
crw-rw-r--. qemu qemu system_u:object_r:svirt_image_t:s0:c319,c628 004

Comment 2 Michal Privoznik 2011-12-22 17:22:14 UTC
Patch sent upstream:

https://www.redhat.com/archives/libvir-list/2011-December/msg00982.html

Comment 3 Michal Privoznik 2011-12-24 17:23:48 UTC
Moving to POST:

commit 8a34f822e6a618a3ad80c81d5f2c76d62faf2f29
Author:     Michal Privoznik <mprivozn>
AuthorDate: Wed Dec 21 18:58:29 2011 +0100
Commit:     Michal Privoznik <mprivozn>
CommitDate: Sat Dec 24 18:12:04 2011 +0100

    qemu: Keep list of USB devices attached to domains
    
    In order to avoid situation where a USB device is
    in use by two domains, we must keep a list of already
    attached devices like we do for PCI.

v0.9.8-102-g8a34f82

Comment 5 weizhang 2012-01-10 11:00:10 UTC
test on 
libvirt-0.9.9-1.el6.x86_64
kernel-2.6.32-225.el6.x86_64
qemu-kvm-0.12.1.2-2.213.el6.x86_64


The bug still exists, both guests can start successfully, so change the bug status to assign

Comment 7 Michal Privoznik 2012-03-23 13:03:36 UTC
(In reply to comment #5)
> test on 
> libvirt-0.9.9-1.el6.x86_64
> kernel-2.6.32-225.el6.x86_64
> qemu-kvm-0.12.1.2-2.213.el6.x86_64
> 
> 
> The bug still exists, both guests can start successfully, so change the bug
> status to assign

Can you please provide full debug logs then? As it works on my machine.

Comment 8 weizhang 2012-03-26 02:36:38 UTC
Created attachment 572614 [details]
start 2 guest with assign the same usb

Comment 9 Michal Privoznik 2012-04-17 11:38:28 UTC
Patches committed upstream:

commit ea3bc548aca7b4c448b48863120ad35a7337c127
Author:     Michal Privoznik <mprivozn>
AuthorDate: Mon Mar 26 16:44:19 2012 +0200
Commit:     Michal Privoznik <mprivozn>
CommitDate: Wed Apr 4 15:09:41 2012 +0200

    qemu: Build activeUsbHostdevs list on process reconnect
    
    If the daemon is restarted it will lose list of active
    USB devices assigned to active domains. Therefore we need
    to rebuild this list on qemuProcessReconnect().

commit e2f5dd6134ebeb6846450c7d7782273d3d274859
Author:     Michal Privoznik <mprivozn>
AuthorDate: Mon Mar 26 16:40:01 2012 +0200
Commit:     Michal Privoznik <mprivozn>
CommitDate: Wed Apr 4 15:09:41 2012 +0200

    qemu: Delete USB devices used by domain on stop
    
    To prevent assigning one USB device to two domains,
    we keep a list of assigned USB devices. On domain
    startup - qemuProcessStart() - we insert devices
    used by domain into the list but remove them only
    on detach-device. Devices are, however, released
    on qemuProcessStop() as well.

v0.9.11-12-gea3bc54
v0.9.11-11-ge2f5dd6

Therefore moving to POST.

Comment 10 Thomas Davis 2012-04-20 22:29:01 UTC
I just found this; see:

https://bugzilla.redhat.com/show_bug.cgi?id=814866
https://bugzilla.redhat.com/show_bug.cgi?id=798838

for problem(s) caused by this set of patches.

Comment 11 Laine Stump 2012-04-28 18:59:16 UTC
(In reply to comment #10)
> I just found this; see:
> 
> https://bugzilla.redhat.com/show_bug.cgi?id=814866
> https://bugzilla.redhat.com/show_bug.cgi?id=798838
> 
> for problem(s) caused by this set of patches.

My understanding of your comments in Bug 814866 was that the patches detailed in this bug *fixed* the problem, not that they caused it.

I haven't investigated the other report.

Comment 12 Laine Stump 2012-04-30 14:44:17 UTC
*** Bug 798838 has been marked as a duplicate of this bug. ***

Comment 13 Laine Stump 2012-04-30 15:32:50 UTC
Actually, even though it is solved by the two patches listed in Comment 9, Bug 798838 is a DUP of Bug 808459 (which tracks pushing those two patches to RHEL).

Comment 14 Laine Stump 2012-04-30 15:35:24 UTC
The patches detailed in Comment 9, have already been pushed to 6.3 to resolve Bug 808459. So should this be moved back to 6.3 by virtue of all the patches already being pushed? What about the failure detailed in Comment8? Is that addressed by those two patches? (sounds like it is, but would be good to verify before going to the trouble of requesting blocker, etc).

If we do move it to 6.3, can it go straight to ON_QA? The patches have been present in the RHEL builds since 0.9.10-10.

Comment 17 weizhang 2012-05-16 09:10:26 UTC
Test on 
kernel-2.6.32-269.el6.x86_64
qemu-kvm-0.12.1.2-2.292.el6.x86_64
libvirt-0.9.10-20.el6.x86_64

When first time do
# virsh start kvm-rhel6u3-i386
error: Failed to start domain kvm-rhel6u3-i386
error: Requested operation is not valid: USB device 002:002 is in use by domain kvm-rhel6u3-x86_64

it report error

but second time do 
# virsh start kvm-rhel6u3-i386
Domain kvm-rhel6u3-i386 started

it will succeed

I found that after first start, the label changed to
crw-rw-r--. root root system_u:object_r:usb_device_t:s0 002

so reassign it.

Comment 18 weizhang 2012-05-16 09:32:47 UTC
Created attachment 584917 [details]
second guest can start on second time

Comment 19 Michal Privoznik 2012-05-16 13:06:39 UTC
weizhang,

I think it's a regression introduced by fix for Bug 815755. Can you please re-test with -10 build? From the logs I see you've used -20 which contains patches for the bug I am referring to.

Thanks!

Anyway, I'll post patches soon.

Comment 22 weizhang 2012-05-17 03:14:05 UTC
(In reply to comment #19)
> weizhang,
> 
> I think it's a regression introduced by fix for Bug 815755. Can you please
> re-test with -10 build? From the logs I see you've used -20 which contains
> patches for the bug I am referring to.
> 
> Thanks!
> 
> Anyway, I'll post patches soon.

Hi Michal,

I test with libvirt-0.9.10-10 and can not reproduce this problem, so I think it is a regression.

Comment 23 Michal Privoznik 2012-05-17 09:37:21 UTC
Yeah. I've proposed a patch:

http://post-office.corp.redhat.com/archives/rhvirt-patches/2012-May/msg00305.html

Therefore moving to POST.

Comment 24 Michal Privoznik 2012-05-17 15:18:58 UTC
Oh, and this one is needed too:

http://post-office.corp.redhat.com/archives/rhvirt-patches/2012-May/msg00309.html

Comment 26 Daniel Veillard 2012-05-23 13:43:19 UTC
The new build libvirt-0.9.10-21.el6 contains the two patches,

Daniel

Comment 28 weizhang 2012-05-24 10:46:57 UTC
Test on 
kernel-2.6.32-269.el6.x86_64
qemu-kvm-0.12.1.2-2.292.el6.x86_64
libvirt-0.9.10-21.el6.x86_64

After start second guest with usb, it report error
#virsh start kvm-rhel6u3-i386
error: Failed to start domain kvm-rhel6u3-i386
error: Requested operation is not valid: USB device 002:007 is in use by domain kvm-rhel6u3-x86_64

The error will still report when do several times, but after start second guest first, the label of usb device changed back to
crw-rw-r--. root root system_u:object_r:usb_device_t:s0 007

but not as the label when start first guest
crw-rw-r--. qemu qemu system_u:object_r:svirt_image_t:s0:c441,c858 007

Is that ok?

Comment 29 Michal Privoznik 2012-05-24 14:27:38 UTC
No, but I'd say in this phase of release please split that into a different bug, please.

I've taken a look and it's not only USB devices what's affected but PCI as well.

Comment 30 weizhang 2012-05-25 02:13:25 UTC
Thanks Michal, then verify this bug as comment 28 shows, file a new bug https://bugzilla.redhat.com/show_bug.cgi?id=825068

Comment 31 Michal Privoznik 2012-05-25 08:00:18 UTC
*** Bug 796059 has been marked as a duplicate of this bug. ***

Comment 33 errata-xmlrpc 2012-06-20 06:32:04 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.

http://rhn.redhat.com/errata/RHSA-2012-0748.html