Bug 1979761

Summary: Can not define mdev device successfully when using xml dumped from mdev device defined by mdevctl
Product: Red Hat Enterprise Linux 9 Reporter: yafu <yafu>
Component: libvirtAssignee: Jonathon Jongsma <jjongsma>
libvirt sub component: General QA Contact: zhentang <zhetang>
Status: CLOSED ERRATA Docs Contact:
Severity: unspecified    
Priority: unspecified CC: jdenemar, lmen, virt-maint, xuzhang, yalzhang, zhguo
Version: 9.0Keywords: AutomationTriaged, Reopened
Target Milestone: rc   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: libvirt-7.7.0-1.el9 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2022-05-17 12:45:04 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 yafu 2021-07-07 01:13:18 UTC
Description of problem:
Can not define mdev device successfully when using xml dumped from mdev device defined by mdevctl

Version-Release number of selected component (if applicable):
libvirt-daemon-7.5.0-1.module+el8.5.0+11664+59f87560.x86_64
qemu-kvm-6.0.0-21.module+el8.5.0+11555+e0ab0d09.x86_64
mdevctl-0.78-1.el8.noarch


How reproducible:
100%

Steps to Reproduce:
1.Define mdev device with mdevctl:
# mdevctl define --uuid=83a54ae7-09b1-4046-b0e2-b31de31505a0 --parent=0000:5b:00.0 --type=nvidia-262 -a

2.List the mdev device:
#virsh nodedev-list --cap mdev --inactive
mdev_83a54ae7_09b1_4046_b0e2_b31de31505a0

3.Dump the mdev device xml:
#virsh nodedev-dumpxml mdev_83a54ae7_09b1_4046_b0e2_b31de31505a0 > mdev-2.xml

4.Check the xml:
#cat mdev-2.xml
<device>
  <name>mdev_83a54ae7_09b1_4046_b0e2_b31de31505a0</name>
  <parent>0000:5b:00.0</parent>
  <capability type='mdev'>
    <type id='nvidia-262'/>
    <uuid>83a54ae7-09b1-4046-b0e2-b31de31505a0</uuid>
    <iommuGroup number='0'/>
  </capability>
</device>

5.Undefine the mdev device:
#virsh undefine mdev_83a54ae7_09b1_4046_b0e2_b31de31505a0

6.Create the mdev device with xml:
# virsh nodedev-define mdev-2.xml 
error: Failed to define node device from 'mdev-2.xml'
error: internal error: invalid use of command API

Actual results:
Can not define mdev device successfully when using xml dumped from mdev device defined by mdevctl.

Expected results:
Should define mdev device successfully when using xml dumped from mdev device defined by mdevctl.

Additional info:
1.Can define mdev device successfully if modifying the parent address from '0000:5b:00.0' to 'pci_0000_5b_00_0'.

Comment 1 Jonathon Jongsma 2021-07-08 18:16:47 UTC
Good catch. Unfortunately this is a bit tricky to fix for all scenarios because of the fact that mdevctl supports defining mdevs for parent devices that do not actually exist (yet) on the host system. 

For active mdevs and for inactive mdevs whose parent devices are present, it's pretty straightforward to convert from '0000:5b:00.0' to 'pci_0000_5b_00_0'. But for those devices whose parents are not present on the host, there's no valid parent id that we can use in that field. I proposed a fix upstream that used the correct parent ID where possible but assigned "parentless" devices to the root "computer" device in libvirt. So for those devices with valid parents, dumping xml and defining should work. But for those without parents, dumping to xml and defining from that xml would still fail because the "computer" device is not a valid parent.

Comment 2 John Ferlan 2021-09-08 13:30:43 UTC
Bulk update: Move RHEL-AV bugs to RHEL9. If necessary to resolve in RHEL8, then clone to the current RHEL8 release.

Comment 3 Jonathon Jongsma 2021-09-14 18:53:39 UTC
The following commits should fully fix this issue. 

Included in release 7.6.0
51fbbfdce8141d5a25edcf66ba3a95ac23b29f94 nodedev: fix parent device of inactive mdevs

Included in release 7.7.0
fdfe4b2837c3408edc57f6d3bf393dc16cd0ac64 nodedev: add internal virNodeDeviceObjListFind()
a9c1febcf43544fb9b9e59e8b45161d11260d9aa nodedev: fix xml output for mdev parents in test suite
e44408dd9c45cb4f5e41caef4a9d2b7d2b8eb528 nodedev: cache parent address in mdev caps
d5ae634ba284fcb1aa40634ad3c0a9e820ec916a nodedev: Add parser validation for node devices
08d29eb3b1d2361b7c70eaf0e269165387cf2756 nodedev: add PostParse callback for nodedev parsing
3bd8181bc5548a0ce81107cbfb480dfdcba5679d nodedev: Handle inactive mdevs with the same UUID
fcae7a44c3b3559d1d8e05543deee41db7a8fdb1 nodedev: look up mdevs by UUID and parent

Since libvirt was recently rebased to 7.7.0, this issue should now be fixed as well.

Comment 7 zhentang 2021-10-15 03:13:41 UTC
Verified on env:
libvirt-7.8.0-1.el9
qemu-kvm-6.1.0-5.el9

Comment 10 zhentang 2021-11-03 07:13:40 UTC
verified on Env:
libvirt-7.8.0-1.el9

Mdev device defined success.

# virsh nodedev-dumpxml mdev_83a54ae7_09b1_4046_b0e2_b31de31505a0_0000_00_02_0 
<device>
  <name>mdev_83a54ae7_09b1_4046_b0e2_b31de31505a0_0000_00_02_0</name>
  <parent>pci_0000_00_02_0</parent>
  <capability type='mdev'>
    <type id='nvidia-262'/>
    <uuid>83a54ae7-09b1-4046-b0e2-b31de31505a0</uuid>
    <iommuGroup number='0'/>
  </capability>
</device>


# virsh nodedev-dumpxml mdev_83a54ae7_09b1_4046_b0e2_b31de31505a0_0000_00_02_0 >mdevtest.xml
# virsh nodedev-undefine mdev_83a54ae7_09b1_4046_b0e2_b31de31505a0_0000_00_02_0 
Undefined node device 'mdev_83a54ae7_09b1_4046_b0e2_b31de31505a0_0000_00_02_0'

# virsh nodedev-define mdevtest.xml 
Node device 'mdev_83a54ae7_09b1_4046_b0e2_b31de31505a0_0000_00_02_0' defined from 'mdevtest.xml'

Comment 12 errata-xmlrpc 2022-05-17 12:45: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 (new packages: libvirt), 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-2022:2390