Commit e6331a32 authored by Paul Burton's avatar Paul Burton

MIPS: KVM: Use prandom_u32_max() to generate tlbwr index

Emulation of the tlbwr instruction, which writes a TLB entry to a random
index in the TLB, currently uses get_random_bytes() to generate a 4 byte
random number which we then mask to form the index. This is overkill in
a couple of ways:

  - We don't need 4 bytes here since we mask the value to form a 6 bit
    number anyway, so we waste /dev/random entropy generating 3 random
    bytes that are unused.

  - We don't need crypto-grade randomness here - the architecture spec
    allows implementations to use any algorithm & merely encourages that
    some pseudo-randomness be used rather than a simple counter. The
    fast prandom_u32() function fits that criteria well.

So rather than using get_random_bytes() & consuming /dev/random entropy,
switch to using the faster prandom_u32_max() which provides what we need
here whilst also performing the masking/modulo for us.
Signed-off-by: default avatarPaul Burton <paul.burton@mips.com>
Reported-by: default avatarGeorge Spelvin <lkml@sdf.org>
Cc: James Hogan <jhogan@kernel.org>
Cc: linux-mips@vger.kernel.org
parent 371a4151
...@@ -1141,9 +1141,7 @@ enum emulation_result kvm_mips_emul_tlbwr(struct kvm_vcpu *vcpu) ...@@ -1141,9 +1141,7 @@ enum emulation_result kvm_mips_emul_tlbwr(struct kvm_vcpu *vcpu)
unsigned long pc = vcpu->arch.pc; unsigned long pc = vcpu->arch.pc;
int index; int index;
get_random_bytes(&index, sizeof(index)); index = prandom_u32_max(KVM_MIPS_GUEST_TLB_SIZE);
index &= (KVM_MIPS_GUEST_TLB_SIZE - 1);
tlb = &vcpu->arch.guest_tlb[index]; tlb = &vcpu->arch.guest_tlb[index];
kvm_mips_invalidate_guest_tlb(vcpu, tlb); kvm_mips_invalidate_guest_tlb(vcpu, tlb);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment