mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Avi Kivity <avi@qumranet.com>
To: kvm-devel@lists.sourceforge.net
Cc: linux-kernel@vger.kernel.org, akpm@osdl.org, mingo@elte.hu
Subject: [PATCH 30/33] KVM: MMU: Detect oom conditions and propagate error to userspace
Date: Thu, 04 Jan 2007 16:19:07 -0000	[thread overview]
Message-ID: <20070104161907.A6B92250048@il.qumranet.com> (raw)
In-Reply-To: <459D21DD.5090506@qumranet.com>

Signed-off-by: Avi Kivity <avi@qumranet.com>

Index: linux-2.6/drivers/kvm/mmu.c
===================================================================
--- linux-2.6.orig/drivers/kvm/mmu.c
+++ linux-2.6/drivers/kvm/mmu.c
@@ -166,19 +166,20 @@ static int is_rmap_pte(u64 pte)
 		== (PT_WRITABLE_MASK | PT_PRESENT_MASK);
 }
 
-static void mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
-				   size_t objsize, int min)
+static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
+				  size_t objsize, int min)
 {
 	void *obj;
 
 	if (cache->nobjs >= min)
-		return;
+		return 0;
 	while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
 		obj = kzalloc(objsize, GFP_NOWAIT);
 		if (!obj)
-			BUG();
+			return -ENOMEM;
 		cache->objects[cache->nobjs++] = obj;
 	}
+	return 0;
 }
 
 static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
@@ -187,12 +188,18 @@ static void mmu_free_memory_cache(struct
 		kfree(mc->objects[--mc->nobjs]);
 }
 
-static void mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
+static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
 {
-	mmu_topup_memory_cache(&vcpu->mmu_pte_chain_cache,
-			       sizeof(struct kvm_pte_chain), 4);
-	mmu_topup_memory_cache(&vcpu->mmu_rmap_desc_cache,
-			       sizeof(struct kvm_rmap_desc), 1);
+	int r;
+
+	r = mmu_topup_memory_cache(&vcpu->mmu_pte_chain_cache,
+				   sizeof(struct kvm_pte_chain), 4);
+	if (r)
+		goto out;
+	r = mmu_topup_memory_cache(&vcpu->mmu_rmap_desc_cache,
+				   sizeof(struct kvm_rmap_desc), 1);
+out:
+	return r;
 }
 
 static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
@@ -824,8 +831,11 @@ static int nonpaging_page_fault(struct k
 {
 	gpa_t addr = gva;
 	hpa_t paddr;
+	int r;
 
-	mmu_topup_memory_caches(vcpu);
+	r = mmu_topup_memory_caches(vcpu);
+	if (r)
+		return r;
 
 	ASSERT(vcpu);
 	ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
@@ -1052,7 +1062,7 @@ int kvm_mmu_reset_context(struct kvm_vcp
 	r = init_kvm_mmu(vcpu);
 	if (r < 0)
 		goto out;
-	mmu_topup_memory_caches(vcpu);
+	r = mmu_topup_memory_caches(vcpu);
 out:
 	return r;
 }
Index: linux-2.6/drivers/kvm/svm.c
===================================================================
--- linux-2.6.orig/drivers/kvm/svm.c
+++ linux-2.6/drivers/kvm/svm.c
@@ -852,6 +852,7 @@ static int pf_interception(struct kvm_vc
 	u64 fault_address;
 	u32 error_code;
 	enum emulation_result er;
+	int r;
 
 	if (is_external_interrupt(exit_int_info))
 		push_irq(vcpu, exit_int_info & SVM_EVTINJ_VEC_MASK);
@@ -860,7 +861,12 @@ static int pf_interception(struct kvm_vc
 
 	fault_address  = vcpu->svm->vmcb->control.exit_info_2;
 	error_code = vcpu->svm->vmcb->control.exit_info_1;
-	if (!kvm_mmu_page_fault(vcpu, fault_address, error_code)) {
+	r = kvm_mmu_page_fault(vcpu, fault_address, error_code);
+	if (r < 0) {
+		spin_unlock(&vcpu->kvm->lock);
+		return r;
+	}
+	if (!r) {
 		spin_unlock(&vcpu->kvm->lock);
 		return 1;
 	}
@@ -1398,6 +1404,7 @@ static int svm_vcpu_run(struct kvm_vcpu 
 	u16 fs_selector;
 	u16 gs_selector;
 	u16 ldt_selector;
+	int r;
 
 again:
 	do_interrupt_requests(vcpu, kvm_run);
@@ -1565,7 +1572,8 @@ again:
 		return 0;
 	}
 
-	if (handle_exit(vcpu, kvm_run)) {
+	r = handle_exit(vcpu, kvm_run);
+	if (r > 0) {
 		if (signal_pending(current)) {
 			++kvm_stat.signal_exits;
 			post_kvm_run_save(vcpu, kvm_run);
@@ -1581,7 +1589,7 @@ again:
 		goto again;
 	}
 	post_kvm_run_save(vcpu, kvm_run);
-	return 0;
+	return r;
 }
 
 static void svm_flush_tlb(struct kvm_vcpu *vcpu)
Index: linux-2.6/drivers/kvm/paging_tmpl.h
===================================================================
--- linux-2.6.orig/drivers/kvm/paging_tmpl.h
+++ linux-2.6/drivers/kvm/paging_tmpl.h
@@ -339,7 +339,8 @@ static int FNAME(fix_write_pf)(struct kv
  *   - normal guest page fault due to the guest pte marked not present, not
  *     writable, or not executable
  *
- *  Returns: 1 if we need to emulate the instruction, 0 otherwise
+ *  Returns: 1 if we need to emulate the instruction, 0 otherwise, or
+ *           a negative value on error.
  */
 static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
 			       u32 error_code)
@@ -351,10 +352,13 @@ static int FNAME(page_fault)(struct kvm_
 	u64 *shadow_pte;
 	int fixed;
 	int write_pt = 0;
+	int r;
 
 	pgprintk("%s: addr %lx err %x\n", __FUNCTION__, addr, error_code);
 
-	mmu_topup_memory_caches(vcpu);
+	r = mmu_topup_memory_caches(vcpu);
+	if (r)
+		return r;
 
 	/*
 	 * Look up the shadow pte for the faulting address.
Index: linux-2.6/drivers/kvm/vmx.c
===================================================================
--- linux-2.6.orig/drivers/kvm/vmx.c
+++ linux-2.6/drivers/kvm/vmx.c
@@ -1289,6 +1289,7 @@ static int handle_exception(struct kvm_v
 	unsigned long cr2, rip;
 	u32 vect_info;
 	enum emulation_result er;
+	int r;
 
 	vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
 	intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
@@ -1317,7 +1318,12 @@ static int handle_exception(struct kvm_v
 		cr2 = vmcs_readl(EXIT_QUALIFICATION);
 
 		spin_lock(&vcpu->kvm->lock);
-		if (!kvm_mmu_page_fault(vcpu, cr2, error_code)) {
+		r = kvm_mmu_page_fault(vcpu, cr2, error_code);
+		if (r < 0) {
+			spin_unlock(&vcpu->kvm->lock);
+			return r;
+		}
+		if (!r) {
 			spin_unlock(&vcpu->kvm->lock);
 			return 1;
 		}
@@ -1680,6 +1686,7 @@ static int vmx_vcpu_run(struct kvm_vcpu 
 	u8 fail;
 	u16 fs_sel, gs_sel, ldt_sel;
 	int fs_gs_ldt_reload_needed;
+	int r;
 
 again:
 	/*
@@ -1853,6 +1860,7 @@ again:
 	if (fail) {
 		kvm_run->exit_type = KVM_EXIT_TYPE_FAIL_ENTRY;
 		kvm_run->exit_reason = vmcs_read32(VM_INSTRUCTION_ERROR);
+		r = 0;
 	} else {
 		if (fs_gs_ldt_reload_needed) {
 			load_ldt(ldt_sel);
@@ -1872,7 +1880,8 @@ again:
 		}
 		vcpu->launched = 1;
 		kvm_run->exit_type = KVM_EXIT_TYPE_VM_EXIT;
-		if (kvm_handle_exit(kvm_run, vcpu)) {
+		r = kvm_handle_exit(kvm_run, vcpu);
+		if (r > 0) {
 			/* Give scheduler a change to reschedule. */
 			if (signal_pending(current)) {
 				++kvm_stat.signal_exits;
@@ -1892,7 +1901,7 @@ again:
 	}
 
 	post_kvm_run_save(vcpu, kvm_run);
-	return 0;
+	return r;
 }
 
 static void vmx_flush_tlb(struct kvm_vcpu *vcpu)

  parent reply	other threads:[~2007-01-04 16:19 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-01-04 15:48 [PATCH 0/33] KVM: MMU: Cache shadow page tables Avi Kivity
2007-01-04 15:50 ` [PATCH 1/33] KVM: MMU: Implement simple reverse mapping Avi Kivity
2007-01-04 15:51 ` [PATCH 2/33] KVM: MMU: Teach the page table walker to track guest page table gfns Avi Kivity
2007-01-04 15:52 ` [PATCH 3/33] KVM: MMU: Load the pae pdptrs on cr3 change like the processor does Avi Kivity
2007-01-04 15:53 ` [PATCH 4/33] KVM: MMU: Fold fetch_guest() into init_walker() Avi Kivity
2007-01-04 15:54 ` [PATCH 5/33] KVM: MU: Special treatment for shadow pae root pages Avi Kivity
2007-01-04 15:55 ` [PATCH 6/33] KVM: MMU: Use the guest pdptrs instead of mapping cr3 in pae mode Avi Kivity
2007-01-04 15:56 ` [PATCH 7/33] KVM: MMU: Make the shadow page tables also special-case pae Avi Kivity
2007-01-04 15:57 ` [PATCH 8/33] KVM: MMU: Make kvm_mmu_alloc_page() return a kvm_mmu_page pointer Avi Kivity
2007-01-04 15:58 ` [PATCH 9/33] KVM: MMU: Shadow page table caching Avi Kivity
2007-01-04 15:59 ` [PATCH 10/33] KVM: MMU: Write protect guest pages when a shadow is created for them Avi Kivity
2007-01-04 16:00 ` [PATCH 11/33] KVM: MMU: Let the walker extract the target page gfn from the pte Avi Kivity
2007-01-04 16:01 ` [PATCH 12/33] KVM: MMU: Support emulated writes into RAM Avi Kivity
2007-01-04 16:02 ` [PATCH 13/33] KVM: MMU: Zap shadow page table entries on writes to guest page tables Avi Kivity
2007-01-04 16:03 ` [PATCH 14/33] KVM: MMU: If emulating an instruction fails, try unprotecting the page Avi Kivity
2007-01-04 16:04 ` [PATCH 15/33] KVM: MMU: Implement child shadow unlinking Avi Kivity
2007-01-04 16:05 ` [PATCH 16/33] KVM: MMU: kvm_mmu_put_page() only removes one link to the page Avi Kivity
2007-01-04 16:06 ` [PATCH 17/33] KVM: MMU: oom handling Avi Kivity
2007-01-04 16:07 ` [PATCH 18/33] KVM: MMU: Remove invlpg interception Avi Kivity
2007-01-04 16:08 ` [PATCH 19/33] KVM: MMU: Remove release_pt_page_64() Avi Kivity
2007-01-04 16:09 ` [PATCH 20/33] KVM: MMU: Handle misaligned accesses to write protected guest page tables Avi Kivity
2007-01-04 16:10 ` [PATCH 21/33] KVM: MMU: <ove is_empty_shadow_page() above kvm_mmu_free_page() Avi Kivity
2007-01-04 16:11 ` [PATCH 22/33] KVM: MMU: Ensure freed shadow pages are clean Avi Kivity
2007-01-04 16:12 ` [PATCH 23/33] KVM: MMU: If an empty shadow page is not empty, report more info Avi Kivity
2007-01-04 16:13 ` [PATCH 24/33] KVM: MMU: Page table write flood protection Avi Kivity
2007-01-04 16:14 ` [PATCH 25/33] KVM: MMU: Never free a shadow page actively serving as a root Avi Kivity
2007-01-04 16:15 ` [PATCH 26/33] KVM: MMU: Fix cmpxchg8b emulation Avi Kivity
2007-01-04 16:16 ` [PATCH 27/33] KVM: MMU: Treat user-mode faults as a hint that a page is no longer a page table Avi Kivity
2007-01-04 16:17 ` [PATCH 28/33] KVM: MMU: Free pages on kvm destruction Avi Kivity
2007-01-04 16:18 ` [PATCH 29/33] KVM: MMU: Replace atomic allocations by preallocated objects Avi Kivity
2007-01-04 16:19 ` Avi Kivity [this message]
2007-01-04 16:20 ` [PATCH 31/33] KVM: MMU: Flush guest tlb when reducing permissions on a pte Avi Kivity
2007-01-04 16:21 ` [PATCH 32/33] KVM: MMU: Destroy mmu while we still have a vcpu left Avi Kivity
2007-01-04 16:22 ` [PATCH 33/33] KVM: MMU: add audit code to check mappings, etc are correct Avi Kivity
2007-01-04 17:22 ` [PATCH 0/33] KVM: MMU: Cache shadow page tables Andrew Morton
2007-01-04 17:41   ` Avi Kivity
2007-01-04 18:02     ` Ingo Molnar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20070104161907.A6B92250048@il.qumranet.com \
    --to=avi@qumranet.com \
    --cc=akpm@osdl.org \
    --cc=kvm-devel@lists.sourceforge.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®