KVM: X86: Avoid explictly fetch instruction in x86_decode_insn()
Hi:
Quoted 12 lines · Peter Xu
insn_fetch()will always implicitly refill instruction buffer properly when the buffer is empty, so we don't need to explicitly fetch it even if insn_len==0 forx86_decode_insn().Signed-off-by Peter Xu <[email protected]>---
arch/x86/kvm/emulate.c| 5 -----1 file changed, 5 deletions(-)arch/x86/kvm/emulate.c +0
-0@@ -5175,11 +5175,6 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) ctxt->opcode_len =1 ;
arch/x86/kvm/emulate.c int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) · line 5180
Quoted 10 lines
arch/x86/kvm/emulate.c +0
-5if (insn_len >0 )memcpy (ctxt->fetch.data, insn, insn_len); -else { - rc =__do_insn_fetch_bytes (ctxt,1 ); -if (rc !=X86EMUL_CONTINUE ) - goto done; - }switch (mode) {case X86EMUL_MODE_REAL :
Looks good, thanks. But it seems we should also take care of the comment in __do_insn_fetch_bytes(), as we do not load instruction at the beginning of x86_decode_insn() now, which may be misleading:
/*
* One instruction can only straddle two pages,
* and one has been loaded at the beginning of
* x86_decode_insn. So, if not enough bytes
* still, we must have hit the 15-byte boundary.
*/
if (unlikely(size < op_size))
return emulate_gp(ctxt, 0);
Paolo Bonzini
maintainerQuoted 22 lines · linmiaohe
Hi:
Quoted 20 lines · Peter Xu
insn_fetch()will always implicitly refill instruction buffer properly when the buffer is empty, so we don't need to explicitly fetch it even if insn_len==0 forx86_decode_insn().Signed-off-by Peter Xu <[email protected]>---
arch/x86/kvm/emulate.c| 5 -----1 file changed, 5 deletions(-)arch/x86/kvm/emulate.c +0
-5@@ -5175,11 +5175,6 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) ctxt->opcode_len =1 ;if (insn_len >0 )memcpy (ctxt->fetch.data, insn, insn_len); -else { - rc =__do_insn_fetch_bytes (ctxt,1 ); -if (rc !=X86EMUL_CONTINUE ) - goto done; - }
Quoted 2 lines
arch/x86/kvm/emulate.c int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) · line 5180
Quoted 2 lines
arch/x86/kvm/emulate.c +0
-0switch (mode) {case X86EMUL_MODE_REAL :
This is a a small (but measurable) optimization; going through __do_insn_fetch_bytes instead of do_insn_fetch_bytes is a little bit faster because it lets you mark the branch in do_insn_fetch_bytes as unlikely, and in general it allows the branch predictor to do a better job.
Paolo
Quoted 11 lines
Looks good, thanks. But it seems we should also take care of the comment in
__do_insn_fetch_bytes(), as we do not load instruction at the beginning ofx86_decode_insn()now, which may be misleading:
/*
* One instruction can only straddle two pages,
* and one has been loaded at the beginning of
*x86_decode_insn. So, if not enough bytes
* still, we must have hit the 15-byte boundary.
*/
if (unlikely(size < op_size))
returnemulate_gp(ctxt, 0);
Quoted 23 lines · Paolo Bonzini
Quoted 22 lines · linmiaohe
Hi:
Quoted 20 lines · Peter Xu
insn_fetch()will always implicitly refill instruction buffer properly when the buffer is empty, so we don't need to explicitly fetch it even if insn_len==0 forx86_decode_insn().Signed-off-by Peter Xu <[email protected]>---
arch/x86/kvm/emulate.c| 5 -----1 file changed, 5 deletions(-)arch/x86/kvm/emulate.c +0
-5@@ -5175,11 +5175,6 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) ctxt->opcode_len =1 ;if (insn_len >0 )memcpy (ctxt->fetch.data, insn, insn_len); -else { - rc =__do_insn_fetch_bytes (ctxt,1 ); -if (rc !=X86EMUL_CONTINUE ) - goto done; - }
Quoted 7 lines
Quoted 2 lines
arch/x86/kvm/emulate.c int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) · line 5180
Quoted 2 lines
arch/x86/kvm/emulate.c +0
-0switch (mode) {case X86EMUL_MODE_REAL :This is a a small (but measurable) optimization; going through
__do_insn_fetch_bytesinstead ofdo_insn_fetch_bytesis a little bit faster because it lets you mark the branch indo_insn_fetch_bytesas unlikely, and in general it allows the branch predictor to do a better job.
Ah I see, that makes sense. Thanks!
Signature · 1 line
Peter Xu
Quoted 22 lines · linmiaohe
Hi:
Quoted 20 lines · Peter Xu
insn_fetch()will always implicitly refill instruction buffer properly when the buffer is empty, so we don't need to explicitly fetch it even if insn_len==0 forx86_decode_insn().Signed-off-by Peter Xu <[email protected]>---
arch/x86/kvm/emulate.c| 5 -----1 file changed, 5 deletions(-)arch/x86/kvm/emulate.c +0
-5@@ -5175,11 +5175,6 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) ctxt->opcode_len =1 ;if (insn_len >0 )memcpy (ctxt->fetch.data, insn, insn_len); -else { - rc =__do_insn_fetch_bytes (ctxt,1 ); -if (rc !=X86EMUL_CONTINUE ) - goto done; - }
Quoted 13 lines
arch/x86/kvm/emulate.c int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) · line 5180
Quoted 2 lines
arch/x86/kvm/emulate.c +0
-0switch (mode) {case X86EMUL_MODE_REAL :Looks good, thanks. But it seems we should also take care of the comment in
__do_insn_fetch_bytes(), as we do not load instruction at the beginning ofx86_decode_insn()now, which may be misleading:
/*
* One instruction can only straddle two pages,
* and one has been loaded at the beginning of
*x86_decode_insn. So, if not enough bytes
* still, we must have hit the 15-byte boundary.
*/
if (unlikely(size < op_size))
returnemulate_gp(ctxt, 0);
Right, thanks for spotting that (even if the patch to be dropped :).
I guess not only the comment, but the check might even fail if we apply the patch. Because when the fetch is the 1st attempt and unluckily that acrosses one page boundary (because we'll only fetch until either 15 bytes or the page boundary), so that single fetch could be smaller than op_size provided.
Thanks,
Signature · 1 line
Peter Xu
Paolo Bonzini
maintainerQuoted 10 lines · Peter Xu
Quoted 10 lines
Looks good, thanks. But it seems we should also take care of the comment in
__do_insn_fetch_bytes(), as we do not load instruction at the beginning ofx86_decode_insn()now, which may be misleading:
/*
* One instruction can only straddle two pages,
* and one has been loaded at the beginning of
*x86_decode_insn. So, if not enough bytes
* still, we must have hit the 15-byte boundary.
*/
if (unlikely(size < op_size))
returnemulate_gp(ctxt, 0);
Quoted 7 lines
Right, thanks for spotting that (even if the patch to be dropped :).
I guess not only the comment, but the check might even fail if we apply the patch. Because when the fetch is the 1st attempt and unluckily that acrosses one page boundary (because we'll only fetch until either 15 bytes or the page boundary), so that single fetch could be smaller than
op_sizeprovided.
Right, priming the decode cache with one byte from the current page cannot fail, and then we know that the next call must be at the beginning of the next page.
Paolo
Quoted 21 lines · Paolo Bonzini
Quoted 10 lines · Peter Xu
Quoted 10 lines
Looks good, thanks. But it seems we should also take care of the comment in
__do_insn_fetch_bytes(), as we do not load instruction at the beginning ofx86_decode_insn()now, which may be misleading:
/*
* One instruction can only straddle two pages,
* and one has been loaded at the beginning of
*x86_decode_insn. So, if not enough bytes
* still, we must have hit the 15-byte boundary.
*/
if (unlikely(size < op_size))
returnemulate_gp(ctxt, 0);Quoted 7 lines
Right, thanks for spotting that (even if the patch to be dropped :).
I guess not only the comment, but the check might even fail if we apply the patch. Because when the fetch is the 1st attempt and unluckily that acrosses one page boundary (because we'll only fetch until either 15 bytes or the page boundary), so that single fetch could be smaller than
op_sizeprovided.
Quoted 3 lines
Right, priming the decode cache with one byte from the current page cannot fail, and then we know that the next call must be at the beginning of the next page.
IIRC I encountered (and fixed) a similar KVM bug in the past. It is a shame I never wrote a unit test (and I don’t have time now), but it would be nice to have one.