Skip to contentSkip to conversation

Balloon support for device assignment

RFC In review [email protected] 3 participants 8 messages Feb 17, 2010 – Feb 21, 2010 1 message not in the archive KVM

lore
Share & tools
lore
#0 lore raw

Currently device assignment forces pinning the entire guest memory. The following kernel and qemu patches add
balloon support for device assignment. When the balloon inflates, the corresponding pages are unmapped from the IOMMU and unpinned, and accordingly they are remapped and pinned when the balloon deflates.

The kernel patch applies to tag v2.6.32
Comments appreciated.

Regards,
Eran.

#1 lore raw

RFC 1/1 KVM: Balloon support for device assignment

From: Eran Borovik <[email protected]>

This patch adds modifications to allow correct
balloon operation when a virtual guest uses a direct assigned device.
The modifications include a new interface between qemu and kvm to allow mapping and unmapping the pages from the IOMMU as well as pinning and unpinning as needed.

---
 include/linux/kvm.h      |    3 ++
 include/linux/kvm_host.h |    4 ++
 virt/kvm/iommu.c         |   86 +++++++++++++++++++++++++++++++++++++++++++--
 virt/kvm/kvm_main.c      |    9 +++++
 4 files changed, 98 insertions(+), 4 deletions(-)
include/linux/kvm.h +3 -0
index f8f8900..567f5f8 100644
@@ -514,6 +514,9 @@ struct kvm_irqfd {
 					struct kvm_userspace_memory_region)
 #define KVM_SET_TSS_ADDR          _IO(KVMIO, 0x47)
 #define KVM_SET_IDENTITY_MAP_ADDR _IOW(KVMIO, 0x48, __u64)
+#define KVM_IOMMU_UNMAP_PAGE  _IOW(KVMIO, 0x49, __u64)
+#define KVM_IOMMU_MAP_PAGE  _IOW(KVMIO, 0x50, __u64)
+
 /* Device model IOC */
 #define KVM_CREATE_IRQCHIP	  _IO(KVMIO,  0x60)
 #define KVM_IRQ_LINE		  _IOW(KVMIO, 0x61, struct kvm_irq_level)
include/linux/kvm_host.h +4 -0
index b7bbb5d..ad904ec 100644
@@ -411,6 +411,10 @@ int kvm_assign_device(struct kvm *kvm,
 		      struct kvm_assigned_dev_kernel *assigned_dev);
 int kvm_deassign_device(struct kvm *kvm,
 			struct kvm_assigned_dev_kernel *assigned_dev);
+void kvm_iommu_unmap_page(struct kvm *kvm,
+			  gfn_t base_gfn);
+int kvm_iommu_map_page(struct kvm *kvm,
+		       gfn_t base_gfn);
 #else /* CONFIG_IOMMU_API */
 static inline int kvm_iommu_map_pages(struct kvm *kvm,
 				      gfn_t base_gfn,
virt/kvm/iommu.c +82 -4

105 lines not on this page · raw message

virt/kvm/kvm_main.c +9 -0
index 7495ce3..560a3ff 100644
@@ -2184,6 +2184,15 @@ static long kvm_vm_ioctl(struct file *filp,
 		r = -EOPNOTSUPP;
 		break;
 	}
+	case KVM_IOMMU_MAP_PAGE: {
+		r = kvm_iommu_map_page(kvm, arg);
+		break;
+	}
+	case KVM_IOMMU_UNMAP_PAGE:{
+		kvm_iommu_unmap_page(kvm, arg);
+		r = 0;
+		break;
+	}
 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
 	case KVM_ASSIGN_DEV_IRQ: {
 		struct kvm_assigned_irq assigned_irq;
Signature · 1 line
1.6.0.4
#2 lore raw

RFC 1/1 QEMU: Balloon support for device assignment

From: Eran Borovik <[email protected]>

This patch adds modifications to allow correct
balloon operation when a virtual guest uses a direct assigned device.
The modifications include a new interface between qemu and kvm to allow mapping and unmapping the pages from the IOMMU as well as pinning and unpinning as needed.

---
 hw/virtio-balloon.c     |   13 ++++++++++---
 kvm/include/linux/kvm.h |    2 ++
 kvm/libkvm/libkvm.h     |    4 ++++
 qemu-kvm.c              |   10 ++++++++++
 qemu-kvm.h              |    4 ++++
 5 files changed, 30 insertions(+), 3 deletions(-)
hw/virtio-balloon.c +10 -3
index 3792012..337f717 100644
@@ -132,6 +132,7 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
                                     elem.out_sg, elem.out_num) == 4) {
             ram_addr_t pa;
             ram_addr_t addr;
+	    bool deflate;
 
             pa = (ram_addr_t)ldl_p(&pfn) << VIRTIO_BALLOON_PFN_SHIFT;
             offset += 4;
@@ -139,12 +140,18 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
             addr = cpu_get_physical_page_desc(pa);
             if ((addr & ~TARGET_PAGE_MASK) != IO_MEM_RAM)
                 continue;
+	    deflate = !!(vq == s->dvq);
+#	    ifdef KVM_CAP_DEVICE_ASSIGNMENT
+	    if (deflate)
+		kvm_map_pfn(NULL, pfn);
+	    else
+		kvm_unmap_pfn(NULL, pfn);
+#	    endif
 
             /* Using qemu_get_ram_ptr is bending the rules a bit, but
                should be OK because we only want a single page.  */
-            balloon_page(qemu_get_ram_ptr(addr), !!(vq == s->dvq));
-        }
-
+	    balloon_page(qemu_get_ram_ptr(addr), deflate);
+	}
         virtqueue_push(vq, &elem, offset);
         virtio_notify(vdev, vq);
     }
kvm/include/linux/kvm.h +2 -0
index 6485981..90f7723 100644
@@ -595,6 +595,8 @@ struct kvm_clock_data {
 					struct kvm_userspace_memory_region)
 #define KVM_SET_TSS_ADDR          _IO(KVMIO,   0x47)
 #define KVM_SET_IDENTITY_MAP_ADDR _IOW(KVMIO,  0x48, __u64)
+#define KVM_IOMMU_UNMAP_PAGE  _IOW(KVMIO, 0x49, __u64)
+#define KVM_IOMMU_MAP_PAGE  _IOW(KVMIO, 0x50, __u64)
 /* Device model IOC */
 #define KVM_CREATE_IRQCHIP        _IO(KVMIO,   0x60)
 #define KVM_IRQ_LINE              _IOW(KVMIO,  0x61, struct kvm_irq_level)
kvm/libkvm/libkvm.h +4 -0
index 4821a1e..7fa83b5 100644
@@ -714,6 +714,10 @@ int kvm_s390_store_status(kvm_context_t kvm, int slot, unsigned long addr);
 int kvm_assign_pci_device(kvm_context_t kvm,
 			  struct kvm_assigned_pci_dev *assigned_dev);
 
+int kvm_deflate_pfn(kvm_context_t kvm, uint32_t pfn);
+
+int kvm_inflate_pfn(kvm_context_t kvm, uint32_t pfn);
+
 /*!
  * \brief Assign IRQ for an assigned device
  *
qemu-kvm.c +10 -0
index a305907..a5ca029 100644
@@ -1081,6 +1081,16 @@ static int kvm_old_assign_irq(kvm_context_t kvm,
     return kvm_vm_ioctl(kvm_state, KVM_ASSIGN_IRQ, assigned_irq);
 }
 
+int kvm_unmap_pfn(kvm_context_t kvm, uint32_t pfn)
+{
+       return kvm_vm_ioctl(kvm_state, KVM_IOMMU_UNMAP_PAGE, pfn);
+}
+
+int kvm_map_pfn(kvm_context_t kvm, uint32_t pfn)
+{
+       return kvm_vm_ioctl(kvm_state, KVM_IOMMU_MAP_PAGE, pfn);
+}
+
 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
 int kvm_assign_irq(kvm_context_t kvm, struct kvm_assigned_irq *assigned_irq)
 {
qemu-kvm.h +4 -0
index 6b3e5a1..861c336 100644
@@ -691,6 +691,10 @@ int kvm_s390_store_status(kvm_context_t kvm, int slot, unsigned long addr);
 int kvm_assign_pci_device(kvm_context_t kvm,
                           struct kvm_assigned_pci_dev *assigned_dev);
 
+int kvm_unmap_pfn(kvm_context_t kvm, uint32_t pfn);
+
+int kvm_map_pfn(kvm_context_t kvm, uint32_t pfn);
+
 /*!
  * \brief Assign IRQ for an assigned device
  *
Signature · 1 line
1.6.0.4
#3 lore raw

Re: [RFC] QEMU: Balloon support for device assignment

Quoted 7 lines · On 02/17/2010 11:43 AM, [email protected] wrote:

From: Eran Borovik<[email protected]>

This patch adds modifications to allow correct
balloon operation when a virtual guest uses a direct assigned device.
The modifications include a new interface between qemu and kvm to allow mapping and unmapping the pages from the IOMMU as well as pinning and unpinning as needed.

Note, on reset we deflate the balloon completely, since the BIOS and boot loader (and possibly the OS post-reboot) are not aware of ballooning.

Signature · 1 line
error compiling committee.c: too many arguments to function
#4 lore raw

Re: [RFC] KVM: Balloon support for device assignment

In reply to [email protected]
Quoted 7 lines · On 02/17/2010 11:43 AM, [email protected] wrote:

From: Eran Borovik<[email protected]>

This patch adds modifications to allow correct
balloon operation when a virtual guest uses a direct assigned device.
The modifications include a new interface between qemu and kvm to allow mapping and unmapping the pages from the IOMMU as well as pinning and unpinning as needed.

The plan for iommu support is to push it into uio. Instead of kvm managing the iommu directly, I'd like qemu to open a uio device and set up an iommu mapping there, which will just happen to match the kvm memory slots. Similarly, interrupts will be forwarded using irqfds. This will allow using the iommu without kvm, and reduce the amount of special purpose kvm code.

These patches make the transition more difficult which worries me. I know Gerd looked at making the move, but no longer.

Signature · 1 line
error compiling committee.c: too many arguments to function
#5 lore raw

Re: [RFC] KVM: Balloon support for device assignment

Quoted 8 lines · Avi Kivity
Quoted 6 lines · On 02/17/2010 11:43 AM, [email protected] wrote:

From: Eran Borovik<[email protected]>

This patch adds modifications to allow correct
balloon operation when a virtual guest uses a direct assigned device.
The modifications include a new interface between qemu and kvm to allow mapping and unmapping the pages from the IOMMU as well as pinning and unpinning as needed.

Quoted 8 lines

The plan for iommu support is to push it into uio. Instead of kvm managing the iommu directly, I'd like qemu to open a uio device and set up an iommu mapping there, which will just happen to match the kvm memory slots. Similarly, interrupts will be forwarded using irqfds. This will allow using the iommu without kvm, and reduce the amount of special purpose kvm code.

These patches make the transition more difficult which worries me.

That's a fair point, but they also address a real short-coming of the current device assignment code, which pins all of the guest's memory unconditionally. Unless the uio effort is in progress and expected to complete shortly, I would think the benefit of these simple patches trumps the cost.

Cheers,
Muli

#6 lore raw

Re: [RFC] KVM: Balloon support for device assignment

Quoted 18 lines · Muli Ben-Yehuda
Quoted 16 lines · Avi Kivity
Quoted 7 lines · On 02/17/2010 11:43 AM, [email protected] wrote:

From: Eran Borovik<[email protected]>

This patch adds modifications to allow correct
balloon operation when a virtual guest uses a direct assigned device.
The modifications include a new interface between qemu and kvm to allow mapping and unmapping the pages from the IOMMU as well as pinning and unpinning as needed.

The plan for iommu support is to push it into uio. Instead of kvm managing the iommu directly, I'd like qemu to open a uio device and set up an iommu mapping there, which will just happen to match the kvm memory slots. Similarly, interrupts will be forwarded using irqfds. This will allow using the iommu without kvm, and reduce the amount of special purpose kvm code.

Quoted 8 lines
Quoted 2 lines

These patches make the transition more difficult which worries me.

That's a fair point, but they also address a real short-coming of the current device assignment code, which pins all of the guest's memory unconditionally. Unless the uio effort is in progress and expected to complete shortly, I would think the benefit of these simple patches trumps the cost.

I'm not worried about transition cost (that's for whoever makes the transition to pay) but about how the APIs would translate to the new way of doing things.

For example, it might be done using mmu notifiers. We might use the existing madvise(MADV_DONTNEED) as the entry point (though I don't know how we would indicate deflation).

Signature · 1 line
error compiling committee.c: too many arguments to function

A message that is not in the archive (sent off-list or not yet received): [email protected]

#8 lore raw

Re: [RFC] KVM: Balloon support for device assignment

Quoted 7 lines · Muli Ben-Yehuda
Quoted 4 lines · Avi Kivity

I'm not worried about transition cost (that's for whoever makes the transition to pay) but about how the APIs would translate to the new way of doing things.

Quoted 8 lines
Quoted 4 lines

For example, it might be done using mmu notifiers. We might use the existing madvise(MADV_DONTNEED) as the entry point (though I don't know how we would indicate deflation).

I guess we could extend mmu notifiers to notify on any PTE transition, but that seems like a lot of VM complexity for a little gain, given the simple KVM-internal alternatives.

mmu notifiers already notify on any pte transition.

Signature · 1 line
error compiling committee.c: too many arguments to function