login
Header Space

 
 

[PATCH 087/104] KVM: Simplify memory allocation

Score:
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: <kvm-devel@...>
Cc: <linux-kernel@...>, Laurent Vivier <Laurent.Vivier@...>
Date: Monday, September 17, 2007 - 4:32 am

From: Laurent Vivier <Laurent.Vivier@bull.net>

The mutex->splinlock convertion alllows us to make some code simplifications.
As we can keep the lock longer, we don't have to release it and then
have to check if the environment has not been modified before re-taking it. We
can remove kvm->busy and kvm->memory_config_version.

Signed-off-by: Laurent Vivier <Laurent.Vivier@bull.net>
Signed-off-by: Avi Kivity <avi@qumranet.com>
---
 drivers/kvm/kvm.h      |    2 --
 drivers/kvm/kvm_main.c |   38 +++-----------------------------------
 2 files changed, 3 insertions(+), 37 deletions(-)

diff --git a/drivers/kvm/kvm.h b/drivers/kvm/kvm.h
index ee9f8bd..351da40 100644
--- a/drivers/kvm/kvm.h
+++ b/drivers/kvm/kvm.h
@@ -411,8 +411,6 @@ struct kvm {
 	int n_free_mmu_pages;
 	struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];
 	struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
-	int memory_config_version;
-	int busy;
 	unsigned long rmap_overflow;
 	struct list_head vm_list;
 	struct file *filp;
diff --git a/drivers/kvm/kvm_main.c b/drivers/kvm/kvm_main.c
index 49a0ff4..c2c8c93 100644
--- a/drivers/kvm/kvm_main.c
+++ b/drivers/kvm/kvm_main.c
@@ -679,7 +679,6 @@ static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
 	unsigned long i;
 	struct kvm_memory_slot *memslot;
 	struct kvm_memory_slot old, new;
-	int memory_config_version;
 
 	r = -EINVAL;
 	/* General sanity checks */
@@ -699,10 +698,8 @@ static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
 	if (!npages)
 		mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
 
-raced:
 	mutex_lock(&kvm->lock);
 
-	memory_config_version = kvm->memory_config_version;
 	new = old = *memslot;
 
 	new.base_gfn = base_gfn;
@@ -725,11 +722,6 @@ raced:
 		      (base_gfn >= s->base_gfn + s->npages)))
 			goto out_unlock;
 	}
-	/*
-	 * Do memory allocations outside lock.  memory_config_version will
-	 * detect any races.
-	 */
-	mutex_unlock(&kvm->lock);
 
 	/* Deallocate if slot is being removed */
 	if (!npages)
@@ -746,14 +738,14 @@ raced:
 		new.phys_mem = vmalloc(npages * sizeof(struct page *));
 
 		if (!new.phys_mem)
-			goto out_free;
+			goto out_unlock;
 
 		memset(new.phys_mem, 0, npages * sizeof(struct page *));
 		for (i = 0; i < npages; ++i) {
 			new.phys_mem[i] = alloc_page(GFP_HIGHUSER
 						     | __GFP_ZERO);
 			if (!new.phys_mem[i])
-				goto out_free;
+				goto out_unlock;
 			set_page_private(new.phys_mem[i],0);
 		}
 	}
@@ -764,27 +756,14 @@ raced:
 
 		new.dirty_bitmap = vmalloc(dirty_bytes);
 		if (!new.dirty_bitmap)
-			goto out_free;
+			goto out_unlock;
 		memset(new.dirty_bitmap, 0, dirty_bytes);
 	}
 
-	mutex_lock(&kvm->lock);
-
-	if (memory_config_version != kvm->memory_config_version) {
-		mutex_unlock(&kvm->lock);
-		kvm_free_physmem_slot(&new, &old);
-		goto raced;
-	}
-
-	r = -EAGAIN;
-	if (kvm->busy)
-		goto out_unlock;
-
 	if (mem->slot >= kvm->nmemslots)
 		kvm->nmemslots = mem->slot + 1;
 
 	*memslot = new;
-	++kvm->memory_config_version;
 
 	kvm_mmu_slot_remove_write_access(kvm, mem->slot);
 	kvm_flush_remote_tlbs(kvm);
@@ -796,7 +775,6 @@ raced:
 
 out_unlock:
 	mutex_unlock(&kvm->lock);
-out_free:
 	kvm_free_physmem_slot(&new, &old);
 out:
 	return r;
@@ -815,12 +793,6 @@ static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
 
 	mutex_lock(&kvm->lock);
 
-	/*
-	 * Prevent changes to guest memory configuration even while the lock
-	 * is not taken.
-	 */
-	++kvm->busy;
-	mutex_unlock(&kvm->lock);
 	r = -EINVAL;
 	if (log->slot >= KVM_MEMORY_SLOTS)
 		goto out;
@@ -841,18 +813,14 @@ static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
 
 	/* If nothing is dirty, don't bother messing with page tables. */
 	if (any) {
-		mutex_lock(&kvm->lock);
 		kvm_mmu_slot_remove_write_access(kvm, log->slot);
 		kvm_flush_remote_tlbs(kvm);
 		memset(memslot->dirty_bitmap, 0, n);
-		mutex_unlock(&kvm->lock);
 	}
 
 	r = 0;
 
 out:
-	mutex_lock(&kvm->lock);
-	--kvm->busy;
 	mutex_unlock(&kvm->lock);
 	return r;
 }
-- 
1.5.3

-
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
git-send-email creates duplicate Message-Id's, Adrian Bunk, (Mon Sep 17, 11:59 am)
Re: git-send-email creates duplicate Message-Id's, Junio C Hamano, (Mon Sep 17, 4:22 pm)
Re: git-send-email creates duplicate Message-Id's, Matti Aarnio, (Mon Sep 17, 4:47 pm)
[PATCH 023/104] KVM: load_pdptrs() cleanups, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 029/104] KVM: Convert vm lock to a mutex, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 036/104] KVM: Remove kvm_{read,write}_guest(), Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 046/104] KVM: Remove stat_set from debugfs, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 031/104] KVM: VMX: pass vcpu_vmx internally, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 074/104] KVM: pending irq save/restore, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 085/104] KVM: Keep control regs in sync, Avi Kivity, (Mon Sep 17, 4:32 am)
[PATCH 093/104] KVM: x86 emulator: push imm8, Avi Kivity, (Mon Sep 17, 4:32 am)
[PATCH 094/104] KVM: x86 emulator: call near, Avi Kivity, (Mon Sep 17, 4:32 am)
[PATCH 095/104] KVM: x86 emulator: pushf, Avi Kivity, (Mon Sep 17, 4:32 am)
[PATCH 103/104] KVM: x86 emulator: popf, Avi Kivity, (Mon Sep 17, 4:32 am)
[PATCH 100/104] KVM: x86 emulator: lea, Avi Kivity, (Mon Sep 17, 4:32 am)
[PATCH 101/104] KVM: x86 emulator: jmp abs, Avi Kivity, (Mon Sep 17, 4:32 am)
[PATCH 087/104] KVM: Simplify memory allocation, Avi Kivity, (Mon Sep 17, 4:32 am)
[PATCH 066/104] KVM: Emulate local APIC in kernel, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 061/104] KVM: Support more memory slots, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 067/104] KVM: In-kernel I/O APIC model, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 068/104] KVM: Emulate hlt in the kernel, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 025/104] KVM: Dynamically allocate vcpus, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 053/104] KVM: Clean up kvm_setup_pio(), Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 051/104] KVM: Remove useless assignment, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 033/104] KVM: SVM: de-containization, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 040/104] KVM: VMX: Add cpu consistency check, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 042/104] KVM: Cleanup mark_page_dirty, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 032/104] KVM: Remove three magic numbers, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 027/104] KVM: add hypercall nr to kvm_run, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 001/104] KVM: Fix *nopage() in kvm_main.c, Avi Kivity, (Mon Sep 17, 4:30 am)
Re: [PATCH 001/104] KVM: Fix *nopage() in kvm_main.c, Christoph Hellwig, (Mon Sep 17, 5:13 am)
Re: [PATCH 001/104] KVM: Fix *nopage() in kvm_main.c, Avi Kivity, (Mon Sep 17, 5:15 am)
Re: [PATCH 001/104] KVM: Fix *nopage() in kvm_main.c, Avi Kivity, (Mon Sep 17, 5:18 am)
Re: [PATCH 001/104] KVM: Fix *nopage() in kvm_main.c, Nick Piggin, (Sun Sep 16, 5:29 pm)
Re: [PATCH 001/104] KVM: Fix *nopage() in kvm_main.c, Avi Kivity, (Mon Sep 17, 2:19 pm)
Re: [PATCH 001/104] KVM: Fix *nopage() in kvm_main.c, Nick Piggin, (Mon Sep 17, 1:17 pm)
Re: [PATCH 001/104] KVM: Fix *nopage() in kvm_main.c, Avi Kivity, (Tue Sep 18, 6:44 am)
speck-geostationary