Avi,

This patch fixes the destruction path of a userspace physical memory slot, which is required for direct mmio for passthrough devices. The problem was that the destruction was done by calling kvm_create_phys_mem with length 0. kvm_create_phys_mem was calling kvm_create_userspace_phys_mem, which calls mmap with lenght 0, which failed.
This patch applies to the main kvm userspace tree.
The patch was tested with direct mmio using the pci-passthrough tree.

Regards,
Ben

From 8593ddc76a7109670eb48338eee0e94a3cfaecb2 Mon Sep 17 00:00:00 2001
From: Ben-Ami Yassour <[EMAIL PROTECTED]>
Date: Sun, 8 Jun 2008 10:41:35 +0300
Subject: [PATCH] KVM: fix destroy userspace physical memory slot

Signed-off-by: Ben-Ami Yassour <[EMAIL PROTECTED]>
---
 libkvm/libkvm.c |   34 +++++++++++++++++++++++++++++++++-
 1 files changed, 33 insertions(+), 1 deletions(-)

diff --git a/libkvm/libkvm.c b/libkvm/libkvm.c
index 1e7bbed..a49cbdc 100644
--- a/libkvm/libkvm.c
+++ b/libkvm/libkvm.c
@@ -431,6 +431,32 @@ void *kvm_create_userspace_phys_mem(kvm_context_t kvm, 
unsigned long phys_start,
         return ptr;
 }

+void kvm_destroy_userspace_phys_mem(kvm_context_t kvm,
+                                   unsigned long phys_start)
+{
+       int r;
+       struct kvm_userspace_memory_region memory = {
+               .memory_size = 0,
+               .guest_phys_addr = phys_start,
+               .flags = 0,
+       };
+
+       memory.userspace_addr = 0;
+       memory.slot = get_slot(phys_start);
+
+       if (memory.slot == -1)
+               return;
+
+       r = ioctl(kvm->vm_fd, KVM_SET_USER_MEMORY_REGION, &memory);
+       if (r == -1) {
+               fprintf(stderr, "destroy_userspace_phys_mem: %s",
+                       strerror(errno));
+               return;
+       }
+
+       free_slot(memory.slot);
+}
+
 #endif

 void *kvm_create_phys_mem(kvm_context_t kvm, unsigned long phys_start,
@@ -584,7 +610,13 @@ void kvm_destroy_phys_mem(kvm_context_t kvm, unsigned long 
phys_start,
                        __FUNCTION__, phys_start, slots[slot].phys_addr);
                phys_start = slots[slot].phys_addr;
        }
-       kvm_create_phys_mem(kvm, phys_start, 0, 0, 0);
+
+#ifdef KVM_CAP_USER_MEMORY
+       if (ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_USER_MEMORY) > 0)
+               kvm_destroy_userspace_phys_mem(kvm, phys_start);
+       else
+#endif
+               kvm_create_kernel_phys_mem(kvm, phys_start, 0, 0, 0);
 }

 static int kvm_get_map(kvm_context_t kvm, int ioctl_num, int slot, void *buf)
--
1.5.5.1

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to