Skip to contentSkip to conversation

Emulator Speedups - Optimize Instruction fetches

RFC PATCH In review Bandan Das 2 participants 8 messages May 6, 2014 – May 7, 2014

Subsystems (3)
lore
Share & tools
lore

Bandan Das

author
#0 lore raw

cover Emulator Speedups - Optimize Instruction fetches

My initial attempt at caching gva->gpa->hva translations. Pretty straight forward with details in the individual patches.

I haven't yet looked into if there are other possibilities to speed things up, just thought of sending these out since the numbers are better

567 cycles/emulated jump instruction
718 cycles/emulated move instruction
730 cycles/emulated arithmetic instruction
946 cycles/emulated memory load instruction
956 cycles/emulated memory store instruction
921 cycles/emulated memory RMW instruction

Old realmode.flat numbers from init ctxt changes -
https://lkml.org/lkml/2014/4/16/848

639 cycles/emulated jump instruction (4.3%) 776 cycles/emulated move instruction (7.5%) 791 cycles/emulated arithmetic instruction (11%) 943 cycles/emulated memory load instruction (5.2%) 948 cycles/emulated memory store instruction (7.6%) 929 cycles/emulated memory RMW instruction (9.0%)

Bandan Das (3):
KVM: x86: pass ctxt to fetch helper function
KVM: x86: use memory_prepare in fetch helper function
KVM: x86: cache userspace address for faster fetches

 arch/x86/include/asm/kvm_emulate.h |  7 +++++-
 arch/x86/kvm/x86.c                 | 46 ++++++++++++++++++++++++++++----------
 2 files changed, 40 insertions(+), 13 deletions(-)
Signature · 1 line
1.8.3.1
author
#1 lore raw

RFC PATCH 1/3 KVM: x86: pass ctxt to fetch helper function

In the following patches, our adress caching struct that's embedded within struct x86_emulate_ctxt will need to be accessed

---
 arch/x86/kvm/x86.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
arch/x86/kvm/x86.c +5 -5
index 122410d..17e3d661 100644
@@ -4061,10 +4061,11 @@ gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
 }
 
 static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
-				      struct kvm_vcpu *vcpu, u32 access,
+				      struct x86_emulate_ctxt *ctxt, u32 access,
 				      struct x86_exception *exception)
 {
 	void *data = val;
+	struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
 	int r = X86EMUL_CONTINUE;
 
 	while (bytes) {
@@ -4098,7 +4099,7 @@ static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
 	struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
 	u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
 
-	return kvm_read_guest_virt_helper(addr, val, bytes, vcpu,
+	return kvm_read_guest_virt_helper(addr, val, bytes, ctxt,
 					  access | PFERR_FETCH_MASK,
 					  exception);
 }
@@ -4110,7 +4111,7 @@ int kvm_read_guest_virt(struct x86_emulate_ctxt *ctxt,
 	struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
 	u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
 
-	return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
+	return kvm_read_guest_virt_helper(addr, val, bytes, ctxt, access,
 					  exception);
 }
 EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
@@ -4119,8 +4120,7 @@ static int kvm_read_guest_virt_system(struct x86_emulate_ctxt *ctxt,
 				      gva_t addr, void *val, unsigned int bytes,
 				      struct x86_exception *exception)
 {
-	struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
-	return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, exception);
+	return kvm_read_guest_virt_helper(addr, val, bytes, ctxt, 0, exception);
 }
 
 int kvm_write_guest_virt_system(struct x86_emulate_ctxt *ctxt,
Signature · 1 line
1.8.3.1
author
#2 lore raw

RFC PATCH 2/3 KVM: x86: use memory_prepare in fetch helper function

In reply to Bandan Das

Insn fetch fastpath function. Not that arch.walk_mmu->gva_to_gpa can't be used but let's piggyback on top of interface meant for our purpose

---
 arch/x86/kvm/x86.c | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)
arch/x86/kvm/x86.c +17 -8
index 17e3d661..cf69e3b 100644
@@ -4065,29 +4065,38 @@ static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
 				      struct x86_exception *exception)
 {
 	void *data = val;
-	struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
 	int r = X86EMUL_CONTINUE;
 
 	while (bytes) {
-		gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
-							    exception);
 		unsigned offset = addr & (PAGE_SIZE-1);
 		unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
 		int ret;
+		unsigned long uaddr;
 
-		if (gpa == UNMAPPED_GVA)
-			return X86EMUL_PROPAGATE_FAULT;
-		ret = kvm_read_guest(vcpu->kvm, gpa, data, toread);
+		ret = ctxt->ops->memory_prepare(ctxt, addr, toread,
+						exception, false,
+						NULL, &uaddr);
+		if (ret != X86EMUL_CONTINUE)
+			return ret;
+
+		if (unlikely(kvm_is_error_hva(uaddr))) {
+			r = X86EMUL_PROPAGATE_FAULT;
+			return r;
+		}
+
+		ret = __copy_from_user(data, (void __user *)uaddr, toread);
 		if (ret < 0) {
 			r = X86EMUL_IO_NEEDED;
-			goto out;
+			return r;
 		}
 
+		ctxt->ops->memory_finish(ctxt, NULL, uaddr);
+
 		bytes -= toread;
 		data += toread;
 		addr += toread;
 	}
-out:
+
 	return r;
 }
 
Signature · 1 line
1.8.3.1
maintainer
#3 lore raw

Re: [RFC PATCH 2/3] KVM: x86: use memory_prepare in fetch helper function

Il 06/05/2014 02:40, Bandan Das ha scritto:

Quoted 24 lines

Insn fetch fastpath function. Not that arch.walk_mmu->gva_to_gpa can't be used but let's piggyback on top of interface meant for our purpose

---
 arch/x86/kvm/x86.c | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)
arch/x86/kvm/x86.c +0 -3
index 17e3d661..cf69e3b 100644
@@ -4065,29 +4065,38 @@ static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
 				      struct x86_exception *exception)
 {
 	void *data = val;
-	struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
 	int r = X86EMUL_CONTINUE;
 
 	while (bytes) {
-		gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
-							    exception);
 		unsigned offset = addr & (PAGE_SIZE-1);

arch/x86/kvm/x86.c static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes, · line 4078

Quoted 10 lines
arch/x86/kvm/x86.c +4 -3
 		unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
 		int ret;
+		unsigned long uaddr;
 
-		if (gpa == UNMAPPED_GVA)
-			return X86EMUL_PROPAGATE_FAULT;
-		ret = kvm_read_guest(vcpu->kvm, gpa, data, toread);
+		ret = ctxt->ops->memory_prepare(ctxt, addr, toread,
+						exception, false,
+						NULL, &uaddr);

You can call the function from x86.c directly, no need to go through ctxt->ops.

Quoted 6 lines
arch/x86/kvm/x86.c +6 -0
+		if (ret != X86EMUL_CONTINUE)
+			return ret;
+
+		if (unlikely(kvm_is_error_hva(uaddr))) {
+			r = X86EMUL_PROPAGATE_FAULT;
+			return r;

arch/x86/kvm/x86.c static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes, · line 4093

Quoted 10 lines
arch/x86/kvm/x86.c +5 -1
+		}
+
+		ret = __copy_from_user(data, (void __user *)uaddr, toread);
 		if (ret < 0) {
 			r = X86EMUL_IO_NEEDED;
-			goto out;
+			return r;
 		}
 
+		ctxt->ops->memory_finish(ctxt, NULL, uaddr);

No need to call memory_finish, since you know the implementation (perhaps add a comment).

Paolo

Quoted 10 lines
arch/x86/kvm/x86.c +1 -1
 		bytes -= toread;
 		data += toread;
 		addr += toread;
 	}
-out:
+
 	return r;
 }
 
maintainer
#4 lore raw

Re: [RFC PATCH 2/3] KVM: x86: use memory_prepare in fetch helper function

In reply to Bandan Das

Il 06/05/2014 02:40, Bandan Das ha scritto:

arch/x86/kvm/x86.c static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes, · line 4085

Quoted 10 lines
arch/x86/kvm/x86.c +10 -0
+		ret = ctxt->ops->memory_prepare(ctxt, addr, toread,
+						exception, false,
+						NULL, &uaddr);
+		if (ret != X86EMUL_CONTINUE)
+			return ret;
+
+		if (unlikely(kvm_is_error_hva(uaddr))) {
+			r = X86EMUL_PROPAGATE_FAULT;
+			return r;
+		}

What you are doing here is basically optimizing kvm_read_guest_virt_helper because you know that all reads will be single-page and you do not need the "next_segment" in kvm_read_guest. Good catch, but you can use kvm_read_guest_page instead of going through ctxt->ops->memory_prepare. :)

Paolo

Quoted 1 line
arch/x86/kvm/x86.c +1 -0
+		ret = __copy_from_user(data, (void __user *)uaddr, toread);
author
#5 lore raw

RFC PATCH 3/3 KVM: x86: cache userspace address for faster fetches

In reply to Bandan Das

On every instruction fetch, kvm_read_guest_virt_helper does the gva to gpa translation followed by searching for the memslot. Store the gva hva mapping so that if there's a match we can directly call __copy_from_user()

---
 arch/x86/include/asm/kvm_emulate.h |  7 ++++++-
 arch/x86/kvm/x86.c                 | 33 +++++++++++++++++++++++----------
 2 files changed, 29 insertions(+), 11 deletions(-)
arch/x86/include/asm/kvm_emulate.h +6 -1
index 085d688..20ccde4 100644
@@ -323,10 +323,11 @@ struct x86_emulate_ctxt {
 	int (*execute)(struct x86_emulate_ctxt *ctxt);
 	int (*check_perm)(struct x86_emulate_ctxt *ctxt);
 	/*
-	 * The following five fields are cleared together,
+	 * The following six fields are cleared together,
 	 * the rest are initialized unconditionally in x86_decode_insn
 	 * or elsewhere
 	 */
+	bool addr_cache_valid;
 	u8 rex_prefix;
 	u8 lock_prefix;
 	u8 rep_prefix;
@@ -348,6 +349,10 @@ struct x86_emulate_ctxt {
 	struct fetch_cache fetch;
 	struct read_cache io_read;
 	struct read_cache mem_read;
+	struct {
+		gfn_t gfn;
+		unsigned long uaddr;
+	} addr_cache;
 };
 
 /* Repeat String Operation Prefix */
arch/x86/kvm/x86.c +23 -10
index cf69e3b..7afcfc7 100644
@@ -4072,26 +4072,38 @@ static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
 		unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
 		int ret;
 		unsigned long uaddr;
+		gfn_t gfn = addr >> PAGE_SHIFT;
 
-		ret = ctxt->ops->memory_prepare(ctxt, addr, toread,
-						exception, false,
-						NULL, &uaddr);
-		if (ret != X86EMUL_CONTINUE)
-			return ret;
+		if (ctxt->addr_cache_valid &&
+		    (ctxt->addr_cache.gfn == gfn))
+			uaddr = (ctxt->addr_cache.uaddr << PAGE_SHIFT) +
+				offset_in_page(addr);
+		else {
+			ret = ctxt->ops->memory_prepare(ctxt, addr, toread,
+							exception, false,
+							NULL, &uaddr);
+			if (ret != X86EMUL_CONTINUE)
+				return ret;
+
+			if (unlikely(kvm_is_error_hva(uaddr))) {
+				r = X86EMUL_PROPAGATE_FAULT;
+				return r;
+			}
 
-		if (unlikely(kvm_is_error_hva(uaddr))) {
-			r = X86EMUL_PROPAGATE_FAULT;
-			return r;
+			/* Cache gfn and hva */
+			ctxt->addr_cache.gfn = addr >> PAGE_SHIFT;
+			ctxt->addr_cache.uaddr = uaddr >> PAGE_SHIFT;
+			ctxt->addr_cache_valid = true;
 		}
 
 		ret = __copy_from_user(data, (void __user *)uaddr, toread);
 		if (ret < 0) {
 			r = X86EMUL_IO_NEEDED;
+			/* Where else should we invalidate cache ? */
+			ctxt->ops->memory_finish(ctxt, NULL, uaddr);
 			return r;
 		}
 
-		ctxt->ops->memory_finish(ctxt, NULL, uaddr);
-
 		bytes -= toread;
 		data += toread;
 		addr += toread;
@@ -4339,6 +4351,7 @@ static void emulator_memory_finish(struct x86_emulate_ctxt *ctxt,
 	struct kvm_memory_slot *memslot;
 	gfn_t gfn;
 
+	ctxt->addr_cache_valid = false;
 	if (!opaque)
 		return;
 
Signature · 1 line
1.8.3.1
maintainer
#6 lore raw

Re: [RFC PATCH 3/3] KVM: x86: cache userspace address for faster fetches

Il 06/05/2014 02:40, Bandan Das ha scritto:

Quoted 17 lines

On every instruction fetch, kvm_read_guest_virt_helper does the gva to gpa translation followed by searching for the memslot. Store the gva hva mapping so that if there's a match we can directly call __copy_from_user()

---
 arch/x86/include/asm/kvm_emulate.h |  7 ++++++-
 arch/x86/kvm/x86.c                 | 33 +++++++++++++++++++++++----------
 2 files changed, 29 insertions(+), 11 deletions(-)
arch/x86/include/asm/kvm_emulate.h +0 -0
index 085d688..20ccde4 100644

arch/x86/include/asm/kvm_emulate.h struct x86_emulate_ctxt { · line 330

Quoted 9 lines
arch/x86/include/asm/kvm_emulate.h +2 -1
@@ -323,10 +323,11 @@ struct x86_emulate_ctxt {
 	int (*execute)(struct x86_emulate_ctxt *ctxt);
 	int (*check_perm)(struct x86_emulate_ctxt *ctxt);
 	/*
-	 * The following five fields are cleared together,
+	 * The following six fields are cleared together,
 	 * the rest are initialized unconditionally in x86_decode_insn
 	 * or elsewhere
 	 */
+	bool addr_cache_valid;

You can just set gfn to -1 to invalidate the fetch.

arch/x86/include/asm/kvm_emulate.h struct x86_emulate_ctxt { · line 355

Quoted 11 lines
arch/x86/include/asm/kvm_emulate.h +4 -0
 	u8 rex_prefix;
 	u8 lock_prefix;
 	u8 rep_prefix;
@@ -348,6 +349,10 @@ struct x86_emulate_ctxt {
 	struct fetch_cache fetch;
 	struct read_cache io_read;
 	struct read_cache mem_read;
+	struct {
+		gfn_t gfn;
+		unsigned long uaddr;
+	} addr_cache;

This is not used by emulate.c, so it should be directly in struct kvm_vcpu. You could invalidate it in init_emulate_ctxt, together with emulate_regs_need_sync_from_vcpu.

In the big picture, however, what we really want is to persist the cache across multiple instructions, and also cache the linearization of the address (where you add RIP and CS.base). This would be a bigger source of improvement. If you do that, the cache has to be indeed in x86_emulate_ctxt, but on the other hand the implementation needs to be in emulate.c.

Quoted 16 lines
arch/x86/include/asm/kvm_emulate.h +0 -0
 };
 
 /* Repeat String Operation Prefix */
arch/x86/kvm/x86.c +1 -3
index cf69e3b..7afcfc7 100644
@@ -4072,26 +4072,38 @@ static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
 		unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
 		int ret;
 		unsigned long uaddr;
+		gfn_t gfn = addr >> PAGE_SHIFT;
 
-		ret = ctxt->ops->memory_prepare(ctxt, addr, toread,
-						exception, false,
-						NULL, &uaddr);

arch/x86/kvm/x86.c static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes, · line 4084

Quoted 10 lines
arch/x86/kvm/x86.c +8 -2
-		if (ret != X86EMUL_CONTINUE)
-			return ret;
+		if (ctxt->addr_cache_valid &&
+		    (ctxt->addr_cache.gfn == gfn))
+			uaddr = (ctxt->addr_cache.uaddr << PAGE_SHIFT) +
+				offset_in_page(addr);
+		else {
+			ret = ctxt->ops->memory_prepare(ctxt, addr, toread,
+							exception, false,
+							NULL, &uaddr);

Did you measure the hit rate, and the speedup after every patch? My reading of the code is that the fetch is done only once per page, with the speedup coming from the microoptimization that patch 2 provides for single-page reads. Single-page reads are indeed a very common case for the emulator.

I suggest to start with making patch 2 ready for inclusion.

Paolo

Quoted 39 lines
arch/x86/kvm/x86.c +14 -5
+			if (ret != X86EMUL_CONTINUE)
+				return ret;
+
+			if (unlikely(kvm_is_error_hva(uaddr))) {
+				r = X86EMUL_PROPAGATE_FAULT;
+				return r;
+			}
 
-		if (unlikely(kvm_is_error_hva(uaddr))) {
-			r = X86EMUL_PROPAGATE_FAULT;
-			return r;
+			/* Cache gfn and hva */
+			ctxt->addr_cache.gfn = addr >> PAGE_SHIFT;
+			ctxt->addr_cache.uaddr = uaddr >> PAGE_SHIFT;
+			ctxt->addr_cache_valid = true;
 		}
 
 		ret = __copy_from_user(data, (void __user *)uaddr, toread);
 		if (ret < 0) {
 			r = X86EMUL_IO_NEEDED;
+			/* Where else should we invalidate cache ? */
+			ctxt->ops->memory_finish(ctxt, NULL, uaddr);
 			return r;
 		}
 
-		ctxt->ops->memory_finish(ctxt, NULL, uaddr);
-
 		bytes -= toread;
 		data += toread;
 		addr += toread;
@@ -4339,6 +4351,7 @@ static void emulator_memory_finish(struct x86_emulate_ctxt *ctxt,
 	struct kvm_memory_slot *memslot;
 	gfn_t gfn;
 
+	ctxt->addr_cache_valid = false;
 	if (!opaque)
 		return;
 
author
#7 lore raw

Re: [RFC PATCH 3/3] KVM: x86: cache userspace address for faster fetches

Quoted 30 lines · Paolo Bonzini

Il 06/05/2014 02:40, Bandan Das ha scritto:

Quoted 17 lines

On every instruction fetch, kvm_read_guest_virt_helper does the gva to gpa translation followed by searching for the memslot. Store the gva hva mapping so that if there's a match we can directly call __copy_from_user()

---
 arch/x86/include/asm/kvm_emulate.h |  7 ++++++-
 arch/x86/kvm/x86.c                 | 33 +++++++++++++++++++++++----------
 2 files changed, 29 insertions(+), 11 deletions(-)
arch/x86/include/asm/kvm_emulate.h +0 -0
index 085d688..20ccde4 100644

arch/x86/include/asm/kvm_emulate.h struct x86_emulate_ctxt { · line 330

Quoted 9 lines
arch/x86/include/asm/kvm_emulate.h +2 -1
@@ -323,10 +323,11 @@ struct x86_emulate_ctxt {
 	int (*execute)(struct x86_emulate_ctxt *ctxt);
 	int (*check_perm)(struct x86_emulate_ctxt *ctxt);
 	/*
-	 * The following five fields are cleared together,
+	 * The following six fields are cleared together,
 	 * the rest are initialized unconditionally in x86_decode_insn
 	 * or elsewhere
 	 */
+	bool addr_cache_valid;

You can just set gfn to -1 to invalidate the fetch.

Quoted 15 lines

arch/x86/include/asm/kvm_emulate.h struct x86_emulate_ctxt { · line 355

Quoted 11 lines
arch/x86/include/asm/kvm_emulate.h +4 -0
 	u8 rex_prefix;
 	u8 lock_prefix;
 	u8 rep_prefix;
@@ -348,6 +349,10 @@ struct x86_emulate_ctxt {
 	struct fetch_cache fetch;
 	struct read_cache io_read;
 	struct read_cache mem_read;
+	struct {
+		gfn_t gfn;
+		unsigned long uaddr;
+	} addr_cache;

This is not used by emulate.c, so it should be directly in struct kvm_vcpu. You could invalidate it in init_emulate_ctxt, together with emulate_regs_need_sync_from_vcpu.

Ok.

Quoted 20 lines

In the big picture, however, what we really want is to persist the cache across multiple instructions, and also cache the linearization of the address (where you add RIP and CS.base). This would be a bigger source of improvement. If you do that, the cache has to be indeed in x86_emulate_ctxt, but on the other hand the implementation needs to be in emulate.c.

Quoted 13 lines
arch/x86/include/asm/kvm_emulate.h +0 -0
 };
 
 /* Repeat String Operation Prefix */
arch/x86/kvm/x86.c +1 -0
index cf69e3b..7afcfc7 100644
@@ -4072,26 +4072,38 @@ static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
 		unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
 		int ret;
 		unsigned long uaddr;
+		gfn_t gfn = addr >> PAGE_SHIFT;
 
Quoted 16 lines

arch/x86/kvm/x86.c static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes, · line 4084

Quoted 13 lines
arch/x86/kvm/x86.c +8 -5
-		ret = ctxt->ops->memory_prepare(ctxt, addr, toread,
-						exception, false,
-						NULL, &uaddr);
-		if (ret != X86EMUL_CONTINUE)
-			return ret;
+		if (ctxt->addr_cache_valid &&
+		    (ctxt->addr_cache.gfn == gfn))
+			uaddr = (ctxt->addr_cache.uaddr << PAGE_SHIFT) +
+				offset_in_page(addr);
+		else {
+			ret = ctxt->ops->memory_prepare(ctxt, addr, toread,
+							exception, false,
+							NULL, &uaddr);

Did you measure the hit rate, and the speedup after every patch? My reading of the code is that the fetch is done only once per page, with

Yes, I did. IIRC, patch 3 actually improved the speedup compared to 2. A rough estimate for jmp - patch 2 reduced it to the low 600s, I guess around 610, but I could get a fresh set of numbers.

So, not sure where the speed up is coming from, I will try to find out.

Quoted 47 lines

the speedup coming from the microoptimization that patch 2 provides for single-page reads. Single-page reads are indeed a very common case for the emulator.

I suggest to start with making patch 2 ready for inclusion.

Paolo

Quoted 39 lines
arch/x86/kvm/x86.c +14 -5
+			if (ret != X86EMUL_CONTINUE)
+				return ret;
+
+			if (unlikely(kvm_is_error_hva(uaddr))) {
+				r = X86EMUL_PROPAGATE_FAULT;
+				return r;
+			}
 
-		if (unlikely(kvm_is_error_hva(uaddr))) {
-			r = X86EMUL_PROPAGATE_FAULT;
-			return r;
+			/* Cache gfn and hva */
+			ctxt->addr_cache.gfn = addr >> PAGE_SHIFT;
+			ctxt->addr_cache.uaddr = uaddr >> PAGE_SHIFT;
+			ctxt->addr_cache_valid = true;
 		}
 
 		ret = __copy_from_user(data, (void __user *)uaddr, toread);
 		if (ret < 0) {
 			r = X86EMUL_IO_NEEDED;
+			/* Where else should we invalidate cache ? */
+			ctxt->ops->memory_finish(ctxt, NULL, uaddr);
 			return r;
 		}
 
-		ctxt->ops->memory_finish(ctxt, NULL, uaddr);
-
 		bytes -= toread;
 		data += toread;
 		addr += toread;
@@ -4339,6 +4351,7 @@ static void emulator_memory_finish(struct x86_emulate_ctxt *ctxt,
 	struct kvm_memory_slot *memslot;
 	gfn_t gfn;
 
+	ctxt->addr_cache_valid = false;
 	if (!opaque)
 		return;