RE: [PATCH 1/4] KVM: support VT-d device hotplug

Han, Weidong
2008-11-17T09:16:58Z

Avi Kivity wrote:
> Han, Weidong wrote:
>> From bba614bf2acf22f765995fb2364de04cec039226 Mon Sep 17 00:00:00
>> 2001
>> From: Weidong Han <weidong.han@intel.com>
>> Date: Fri, 14 Nov 2008 16:53:10 +0800
>> Subject: [PATCH] support VT-d device hotplug
>>
>> wrap kvm_assign_device() and kvm_deassign_device() to support
>> assign/deassign a device to a guest
>>
>
>> --- a/include/linux/kvm.h
>> +++ b/include/linux/kvm.h
>> @@ -425,6 +425,8 @@ struct kvm_trace_rec {
>> struct kvm_assigned_pci_dev)
>> #define KVM_ASSIGN_IRQ _IOR(KVMIO, 0x70, \
>> struct kvm_assigned_irq)
>> +#define KVM_DEASSIGN_PCI_DEVICE _IOR(KVMIO, 0x71, \
>> + struct kvm_assigned_pci_dev)
>>
>>
>
> Need a KVM_CAP_ to indicate this is available.
>

KVM_ASSIGN_PCI_DEVICE and KVM_ASSIGN_IRQ don't need a KVM_CAP_xxx. Why do you need it for KVM_DEASSIGN_PCI_DEVICE?

>>
>> static inline void kvm_guest_enter(void)
>> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
>> index 4f43abe..689d615 100644
>> --- a/virt/kvm/kvm_main.c
>> +++ b/virt/kvm/kvm_main.c
>> @@ -293,7 +293,11 @@ static int kvm_vm_ioctl_assign_device(struct
>> kvm *kvm, list_add(&match->list, &kvm->arch.assigned_dev_head);
>>
>> if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
>> - r = kvm_iommu_map_guest(kvm, match);
>> + if (kvm->arch.intel_iommu_domain)
>> + r = kvm_assign_device(kvm, match);
>> + else
>> + r = kvm_iommu_map_guest(kvm, match);
>> +
>>
>
> Shouldn't kvm_assign_device() be called unconditionally? Perhaps some
> code needs to be removed from kvm_iommu_map_guest().

Yes, kvm_iommu_map_guest() just needs to allocate intel_iommu_domain and map memslots. It needs to be called once for a guest. The code will look like:
...
if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
if (!kvm->arch.intel_iommu_domain)
r = kvm_iommu_map_guest(kvm, match);
r = kvm_assign_device(kvm, match);
...

I will update the patch.

Regards,
Weidong