* [PATCH] MIPS: KVM: Use prandom_u32_max() to generate tlbwr index
@ 2019-03-22 18:04 Paul Burton
2019-03-25 21:02 ` Paul Burton
0 siblings, 1 reply; 2+ messages in thread
From: Paul Burton @ 2019-03-22 18:04 UTC (permalink / raw)
To: linux-mips; +Cc: Paul Burton, George Spelvin, James Hogan
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: Paul Burton <paul.burton@mips.com>
Reported-by: George Spelvin <lkml@sdf.org>
Cc: James Hogan <jhogan@kernel.org>
---
arch/mips/kvm/emulate.c | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/arch/mips/kvm/emulate.c b/arch/mips/kvm/emulate.c
index 0074427b04fb..e5de6bac8197 100644
--- a/arch/mips/kvm/emulate.c
+++ b/arch/mips/kvm/emulate.c
@@ -1141,9 +1141,7 @@ enum emulation_result kvm_mips_emul_tlbwr(struct kvm_vcpu *vcpu)
unsigned long pc = vcpu->arch.pc;
int index;
- get_random_bytes(&index, sizeof(index));
- index &= (KVM_MIPS_GUEST_TLB_SIZE - 1);
-
+ index = prandom_u32_max(KVM_MIPS_GUEST_TLB_SIZE);
tlb = &vcpu->arch.guest_tlb[index];
kvm_mips_invalidate_guest_tlb(vcpu, tlb);
--
2.21.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] MIPS: KVM: Use prandom_u32_max() to generate tlbwr index
2019-03-22 18:04 [PATCH] MIPS: KVM: Use prandom_u32_max() to generate tlbwr index Paul Burton
@ 2019-03-25 21:02 ` Paul Burton
0 siblings, 0 replies; 2+ messages in thread
From: Paul Burton @ 2019-03-25 21:02 UTC (permalink / raw)
To: Paul Burton
Cc: linux-mips, Paul Burton, George Spelvin, James Hogan, linux-mips
Hello,
Paul Burton wrote:
> 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: Paul Burton <paul.burton@mips.com>
> Reported-by: George Spelvin <lkml@sdf.org>
> Cc: James Hogan <jhogan@kernel.org>
Applied to mips-next.
Thanks,
Paul
[ This message was auto-generated; if you believe anything is incorrect
then please email paul.burton@mips.com to report it. ]
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2019-03-25 21:02 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-22 18:04 [PATCH] MIPS: KVM: Use prandom_u32_max() to generate tlbwr index Paul Burton
2019-03-25 21:02 ` Paul Burton
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.