mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Luca Tettamanti <kronos.it@gmail.com>
To: Avi Kivity <avi@qumranet.com>
Cc: kvm-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org
Subject: Re: [kvm-devel] [BUG] Oops with KVM-27
Date: Sun, 17 Jun 2007 17:14:52 +0200	[thread overview]
Message-ID: <20070617151452.GA21971@dreamland.darkstar.lan> (raw)
In-Reply-To: <4673949B.1070505@qumranet.com>

Il Sat, Jun 16, 2007 at 10:43:23AM +0300, Avi Kivity ha scritto: 
> Luca Tettamanti wrote:
> > Il Fri, Jun 15, 2007 at 12:06:50PM +0300, Avi Kivity ha scritto: 
> >   
> >>> After a bit of thinking: it's correct but removes an optimization;
> >>> furthermore it may miss other instructions that write to memory mapped
> >>> areas.
> >>> A more proper fix should be "force the writeback if dst.ptr is in some
> >>> kind of mmio area".
> >>>
> >>>       
> >> I think we can just disable the optimization, and (in a separate patch)
> >> add it back in emulator_write_phys(), where we know we're modifying
> >> memory and not hitting mmio.
> >>     
> >
> > Problem is that in emulator_write_phys() we've already lost track of the
> > previous (dst.orig_val) value. I moved up the decision in
> >   
> 
> Actually we haven't; just before the memcpy(), we can put a memcmp() to
> guard the kvm_mmu_pte_write(), which is the really expensive operation,
> especially with guest smp.

Yup, but it seemed wasteful to map (at least when highmem is in use) a
page just to check for something that we already knew. That was a
preemptive optmization though, I haven't actually benchmarked the cost
of setting up the mapping ;-)

[...]
> I think we can simply remove the if ().  For the register case, the
> check is more expensive that the write; for mmio, we don't want it; and
> for memory writes, we can put it in emulator_write_phys().

Ok, this way it's simpler. How does this look:

--- a/kernel/x86_emulate.c	2007-06-15 21:13:51.000000000 +0200
+++ b/kernel/x86_emulate.c	2007-06-17 16:57:50.000000000 +0200
@@ -1057,40 +1057,38 @@
 	}
 
 writeback:
-	if ((d & Mov) || (dst.orig_val != dst.val)) {
-		switch (dst.type) {
-		case OP_REG:
-			/* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
-			switch (dst.bytes) {
-			case 1:
-				*(u8 *)dst.ptr = (u8)dst.val;
-				break;
-			case 2:
-				*(u16 *)dst.ptr = (u16)dst.val;
-				break;
-			case 4:
-				*dst.ptr = (u32)dst.val;
-				break;	/* 64b: zero-ext */
-			case 8:
-				*dst.ptr = dst.val;
-				break;
-			}
+	switch (dst.type) {
+	case OP_REG:
+		/* The 4-byte case *is* correct: in 64-bit mode we zero-extend. */
+		switch (dst.bytes) {
+		case 1:
+			*(u8 *)dst.ptr = (u8)dst.val;
 			break;
-		case OP_MEM:
-			if (lock_prefix)
-				rc = ops->cmpxchg_emulated((unsigned long)dst.
-							   ptr, &dst.orig_val,
-							   &dst.val, dst.bytes,
-							   ctxt);
-			else
-				rc = ops->write_emulated((unsigned long)dst.ptr,
-							 &dst.val, dst.bytes,
-							 ctxt);
-			if (rc != 0)
-				goto done;
-		default:
+		case 2:
+			*(u16 *)dst.ptr = (u16)dst.val;
+			break;
+		case 4:
+			*dst.ptr = (u32)dst.val;
+			break;	/* 64b: zero-ext */
+		case 8:
+			*dst.ptr = dst.val;
 			break;
 		}
+		break;
+	case OP_MEM:
+		if (lock_prefix)
+			rc = ops->cmpxchg_emulated((unsigned long)dst.
+						   ptr, &dst.orig_val,
+						   &dst.val, dst.bytes,
+						   ctxt);
+		else
+			rc = ops->write_emulated((unsigned long)dst.ptr,
+						 &dst.val, dst.bytes,
+						 ctxt);
+		if (rc != 0)
+			goto done;
+	default:
+		break;
 	}
 
 	/* Commit shadow register state. */

--- a/kernel/kvm_main.c	2007-06-15 21:18:08.000000000 +0200
+++ b/kernel/kvm_main.c	2007-06-17 16:59:33.000000000 +0200
@@ -1139,8 +1139,10 @@
 		return 0;
 	mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
 	virt = kmap_atomic(page, KM_USER0);
-	kvm_mmu_pte_write(vcpu, gpa, virt + offset, val, bytes);
-	memcpy(virt + offset_in_page(gpa), val, bytes);
+	if (memcmp(virt + offset_in_page(gpa), val, bytes)) {
+		kvm_mmu_pte_write(vcpu, gpa, virt + offset, val, bytes);
+		memcpy(virt + offset_in_page(gpa), val, bytes);
+	}
 	kunmap_atomic(virt, KM_USER0);
 	return 1;
 }



Luca
-- 
Runtime error 6D at f000:a12f : user incompetente

  reply	other threads:[~2007-06-17 15:15 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-06-03 21:34 Luca Tettamanti
2007-06-04  9:35 ` [kvm-devel] " Avi Kivity
2007-06-04 20:22   ` Luca Tettamanti
2007-06-04 20:51     ` Avi Kivity
2007-06-04 21:22       ` Luca Tettamanti
2007-06-05  7:27         ` Avi Kivity
2007-06-07 19:16           ` Luca
2007-06-10 12:22             ` Avi Kivity
2007-06-10 20:54               ` Luca
2007-06-11  7:44                 ` Avi Kivity
2007-06-11 21:06                   ` Luca
2007-06-12  6:44                     ` Avi Kivity
2007-06-12 17:52                   ` Luca Tettamanti
2007-06-13  8:59                     ` Avi Kivity
2007-06-13 20:49                       ` Luca Tettamanti
2007-06-14  8:26                         ` Avi Kivity
2007-06-14 22:33                           ` Luca Tettamanti
2007-06-14 22:53                           ` Luca Tettamanti
2007-06-14 23:13                             ` Luca Tettamanti
2007-06-14 23:27                               ` Luca
2007-06-15  9:06                                 ` Avi Kivity
2007-06-15 21:49                                   ` Luca Tettamanti
2007-06-16  7:43                                     ` Avi Kivity
2007-06-17 15:14                                       ` Luca Tettamanti [this message]
2007-06-17 15:24                                         ` Avi Kivity
2007-06-17 16:52                                           ` [PATCH 1/2] kvm: Fix x86 emulator writeback Luca Tettamanti
2007-06-17 16:58                                             ` Avi Kivity
2007-06-18 10:07                                             ` Avi Kivity
2007-06-18 11:32                                               ` Avi Kivity
2007-06-19 20:25                                                 ` Luca Tettamanti
2007-06-19 20:41                                                   ` Luca Tettamanti
2007-06-20  7:47                                                     ` Avi Kivity
2007-06-19 20:41                                                   ` [PATCH 2/2] kvm: avoid useless memory write when possible Luca Tettamanti
2007-06-17 16:52                                           ` Luca Tettamanti

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=20070617151452.GA21971@dreamland.darkstar.lan \
    --to=kronos.it@gmail.com \
    --cc=avi@qumranet.com \
    --cc=kvm-devel@lists.sourceforge.net \
    --cc=linux-kernel@vger.kernel.org \
    /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®