Skip to contentSkip to conversation

fix destroying a userspace physical memory slot

PATCH In review Ben-Ami Yassour 2 participants 5 messages Jun 8, 2008 – Jun 9, 2008 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fix destroying a userspace physical memory slot

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

Quoted 1 line

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

---
 libkvm/libkvm.c |   34 +++++++++++++++++++++++++++++++++-
 1 files changed, 33 insertions(+), 1 deletions(-)
libkvm/libkvm.c +33 -1
index 1e7bbed..a49cbdc 100644
@@ -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)
Signature · 1 line
1.5.5.1
#1 lore raw
Quoted 7 lines · Ben-Ami Yassour

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.

Looks good, but doesn't apply. Please check your mail client's whitespace mangling settings (or post as attachment).

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

fix destroy userspace physical memory slot

here it is again, this time sent with git-send-mail.

Regards,
Ben

#3 lore raw

PATCH 1/1 KVM: fix destroy userspace physical memory slot

From: Ben-Ami Yassour <[email protected]>

---
 libkvm/libkvm.c |   34 +++++++++++++++++++++++++++++++++-
 1 files changed, 33 insertions(+), 1 deletions(-)
libkvm/libkvm.c +33 -1
index 1e7bbed..a49cbdc 100644
@@ -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)
Signature · 1 line
1.5.5.1
Applied
#4 lore raw

Re: [PATCH] KVM: fix destroy userspace physical memory slot

Quoted 3 lines · [email protected] wrote:

From: Ben-Ami Yassour <[email protected]>

Applied, thanks.

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.