Skip to contentSkip to conversation

KVM: x86: small optimization for is_mtrr_mask calculation

PATCH In review linmiaohe 3 participants 4 messages Mar 5, 2020

Subsystems (3)
lore
Share & tools
lore

linmiaohe

author
#0 lore raw

PATCH 1/1 KVM: x86: small optimization for is_mtrr_mask calculation

From: Miaohe Lin <[email protected]>

We can get is_mtrr_mask by calculating (msr - 0x200) % 2 directly.

---
 arch/x86/kvm/mtrr.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/mtrr.c +2 -2
index 7f0059aa30e1..a98701d9f2bf 100644
@@ -348,7 +348,7 @@ static void set_var_mtrr_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 	int index, is_mtrr_mask;
 
 	index = (msr - 0x200) / 2;
-	is_mtrr_mask = msr - 0x200 - 2 * index;
+	is_mtrr_mask = (msr - 0x200) % 2;
 	cur = &mtrr_state->var_ranges[index];
 
 	/* remove the entry if it's in the list. */
@@ -424,7 +424,7 @@ int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
 		int is_mtrr_mask;
 
 		index = (msr - 0x200) / 2;
-		is_mtrr_mask = msr - 0x200 - 2 * index;
+		is_mtrr_mask = (msr - 0x200) % 2;
 		if (!is_mtrr_mask)
 			*pdata = vcpu->arch.mtrr_state.var_ranges[index].base;
 		else
Signature · 1 line
2.19.1
maintainer
#1 lore raw
Quoted 24 lines · linmiaohe

From: Miaohe Lin <[email protected]>

We can get is_mtrr_mask by calculating (msr - 0x200) % 2 directly.

---
 arch/x86/kvm/mtrr.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/mtrr.c +1 -1
index 7f0059aa30e1..a98701d9f2bf 100644
@@ -348,7 +348,7 @@ static void set_var_mtrr_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 	int index, is_mtrr_mask;
 
 	index = (msr - 0x200) / 2;
-	is_mtrr_mask = msr - 0x200 - 2 * index;
+	is_mtrr_mask = (msr - 0x200) % 2;
 	cur = &mtrr_state->var_ranges[index];
 
 	/* remove the entry if it's in the list. */

arch/x86/kvm/mtrr.c int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) · line 430

Quoted 8 lines
arch/x86/kvm/mtrr.c +1 -1
@@ -424,7 +424,7 @@ int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
 		int is_mtrr_mask;
 
 		index = (msr - 0x200) / 2;
-		is_mtrr_mask = msr - 0x200 - 2 * index;
+		is_mtrr_mask = (msr - 0x200) % 2;
 		if (!is_mtrr_mask)
 			*pdata = vcpu->arch.mtrr_state.var_ranges[index].base;
 		else

If you're going to do that, might as well use ">> 1" for index instead of "/ 2", and "msr & 1" for is_mtrr_mask.

Paolo

#2 lore raw

From: Paolo Bonzini

Quoted 28 lines

Sent: 05 March 2020 14:36

Quoted 25 lines · linmiaohe

From: Miaohe Lin <[email protected]>

We can get is_mtrr_mask by calculating (msr - 0x200) % 2 directly.

---
 arch/x86/kvm/mtrr.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/mtrr.c +1 -1
index 7f0059aa30e1..a98701d9f2bf 100644
@@ -348,7 +348,7 @@ static void set_var_mtrr_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 	int index, is_mtrr_mask;
 
 	index = (msr - 0x200) / 2;
-	is_mtrr_mask = msr - 0x200 - 2 * index;
+	is_mtrr_mask = (msr - 0x200) % 2;
 	cur = &mtrr_state->var_ranges[index];
 
 	/* remove the entry if it's in the list. */
@@ -424,7 +424,7 @@ int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
 		int is_mtrr_mask;
 
Quoted 10 lines

arch/x86/kvm/mtrr.c int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) · line 430

Quoted 7 lines
arch/x86/kvm/mtrr.c +1 -1
 		index = (msr - 0x200) / 2;
-		is_mtrr_mask = msr - 0x200 - 2 * index;
+		is_mtrr_mask = (msr - 0x200) % 2;
 		if (!is_mtrr_mask)
 			*pdata = vcpu->arch.mtrr_state.var_ranges[index].base;
 		else

If you're going to do that, might as well use ">> 1" for index instead of "/ 2", and "msr & 1" for is_mtrr_mask.

Provided the variables are unsigned it makes little difference whether you use / % or >> &.
At least with / % the two values are the same.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

maintainer
#3 lore raw
Quoted 18 lines · David Laight
Quoted 9 lines
Quoted 9 lines
arch/x86/kvm/mtrr.c +1 -1
 	index = (msr - 0x200) / 2;
-	is_mtrr_mask = msr - 0x200 - 2 * index;
+	is_mtrr_mask = (msr - 0x200) % 2;
 	cur = &mtrr_state->var_ranges[index];
 
 	/* remove the entry if it's in the list. */
@@ -424,7 +424,7 @@ int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
 		int is_mtrr_mask;
 
Quoted 9 lines

arch/x86/kvm/mtrr.c int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) · line 430

Quoted 7 lines
arch/x86/kvm/mtrr.c +1 -1
 		index = (msr - 0x200) / 2;
-		is_mtrr_mask = msr - 0x200 - 2 * index;
+		is_mtrr_mask = (msr - 0x200) % 2;
 		if (!is_mtrr_mask)
 			*pdata = vcpu->arch.mtrr_state.var_ranges[index].base;
 		else

If you're going to do that, might as well use ">> 1" for index instead of "/ 2", and "msr & 1" for is_mtrr_mask.

Quoted 3 lines

Provided the variables are unsigned it makes little difference whether you use / % or >> &.
At least with / % the two values are the same.

Yes, I'm old-fashioned, but also I prefer ">>" and "&" for both signed and unsigned, because if ever I need to switch from unsigned to signed I will get floor-division instead of round-to-zero division (most likely the code doesn't expect negative remainders if it was using unsigned).

(That perhaps also reflects on me working a lot with Smalltalk long before switching to the kernel...).

Paolo