Re: [PATCH] KVM: X86: Avoid explictly fetch instruction in x86_decode_insn()

From: Nadav Amit
Date: Wed Mar 04 2020 - 15:30:35 EST


> On Mar 4, 2020, at 10:19 AM, Paolo Bonzini <pbonzini@xxxxxxxxxx> wrote:
>
> On 04/03/20 16:32, Peter Xu wrote:
>>> 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);
>> 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.
>
> 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.