Re: [PATCH 2/4] KVM: pci device assignment

Amit Shah
2008-07-28T08:34:10Z

* On Tuesday 22 Jul 2008 17:43:53 Ben-Ami Yassour wrote:
> Based on a patch from: Amit Shah <amit.shah@qumranet.com>
>
> This patch adds support for handling PCI devices that are assigned to
> the guest.
>
> The device to be assigned to the guest is registered in the host kernel
> and interrupt delivery is handled. If a device is already assigned, or
> the device driver for it is still loaded on the host, the device
> assignment
> is failed by conveying a -EBUSY reply to the userspace.
>
> Devices that share their interrupt line are not supported at the moment.
>
> By itself, this patch will not make devices work within the guest.
> The VT-d extension is required to enable the device to perform DMA.
> Another alternative is PVDMA.
>
> Signed-off-by: Amit Shah <amit.shah@qumranet.com>
> Signed-off-by: Ben-Ami Yassour <benami@il.ibm.com>
> Signed-off-by: Weidong Han <weidong.han@intel.com>

> + if (pci_enable_device(dev)) {
> + printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
> + r = -EBUSY;
> + goto out_put;
> + }
> + r = pci_request_regions(dev, "kvm_assigned_device");
> + if (r) {
> + printk(KERN_INFO "%s: Could not get access to device regions\n",
> + __func__);
> + goto out_disable;
> + }

A driver might already be loaded for this device. If we fail to get the
regions, it might mean the device was enabled and in use. That's the reason I
didn't originally have a pci_disable_device() when pci_request_regions fails.

> +out:
> + mutex_unlock(&kvm->lock);
> + return r;
> +out_list_del:
> + list_del(&match->list);
> + pci_release_regions(dev);
> +out_disable:
> + pci_disable_device(dev);
> +out_put:
> + pci_dev_put(dev);
> +out_free:
> + kfree(match);
> + mutex_unlock(&kvm->lock);
> + return r;