diff mbox

[2/8] KVM: x86: cleanup kvm_apic_match_*()

Message ID 1422568135-28402-3-git-send-email-rkrcmar@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Radim Krčmář Jan. 29, 2015, 9:48 p.m. UTC
The majority of this patch turns
  result = 0; if (CODE) result = 1; return result;
into
  return CODE;
because we return bool now.

Signed-off-by: Radim Kr?má? <rkrcmar@redhat.com>
---
 arch/x86/kvm/lapic.c | 44 +++++++++++++++-----------------------------
 1 file changed, 15 insertions(+), 29 deletions(-)

Comments

Paolo Bonzini Jan. 30, 2015, 8:52 a.m. UTC | #1
On 29/01/2015 22:48, Radim Kr?má? wrote:
> The majority of this patch turns
>   result = 0; if (CODE) result = 1; return result;
> into
>   return CODE;
> because we return bool now.

Added a bunch of "!= 0" to avoid automagic conversion to bool.

Paolo

> Signed-off-by: Radim Kr?má? <rkrcmar@redhat.com>
> ---
>  arch/x86/kvm/lapic.c | 44 +++++++++++++++-----------------------------
>  1 file changed, 15 insertions(+), 29 deletions(-)
> 
> diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
> index 478dd8bd653b..271e7d076879 100644
> --- a/arch/x86/kvm/lapic.c
> +++ b/arch/x86/kvm/lapic.c
> @@ -594,42 +594,34 @@ static bool kvm_apic_match_physical_addr(struct kvm_lapic *apic, u32 dest)
>  
>  static bool kvm_apic_match_logical_addr(struct kvm_lapic *apic, u32 mda)
>  {
> -	int result = 0;
>  	u32 logical_id;
>  
>  	if (kvm_apic_broadcast(apic, mda))
> -		return 1;
> +		return true;
>  
> -	if (apic_x2apic_mode(apic)) {
> -		logical_id = kvm_apic_get_reg(apic, APIC_LDR);
> +	logical_id = kvm_apic_get_reg(apic, APIC_LDR);
> +
> +	if (apic_x2apic_mode(apic))
>  		return logical_id & mda;
> -	}
>  
> -	logical_id = GET_APIC_LOGICAL_ID(kvm_apic_get_reg(apic, APIC_LDR));
> +	logical_id = GET_APIC_LOGICAL_ID(logical_id);
>  
>  	switch (kvm_apic_get_reg(apic, APIC_DFR)) {
>  	case APIC_DFR_FLAT:
> -		if (logical_id & mda)
> -			result = 1;
> -		break;
> +		return logical_id & mda;
>  	case APIC_DFR_CLUSTER:
> -		if (((logical_id >> 4) == (mda >> 0x4))
> -		    && (logical_id & mda & 0xf))
> -			result = 1;
> -		break;
> +		return ((logical_id >> 4) == (mda >> 4))
> +		       && (logical_id & mda & 0xf);
>  	default:
>  		apic_debug("Bad DFR vcpu %d: %08x\n",
>  			   apic->vcpu->vcpu_id, kvm_apic_get_reg(apic, APIC_DFR));
> -		break;
> +		return false;
>  	}
> -
> -	return result;
>  }
>  
>  bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
>  			   int short_hand, unsigned int dest, int dest_mode)
>  {
> -	int result = 0;
>  	struct kvm_lapic *target = vcpu->arch.apic;
>  
>  	apic_debug("target %p, source %p, dest 0x%x, "
> @@ -641,27 +633,21 @@ bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
>  	case APIC_DEST_NOSHORT:
>  		if (dest_mode == 0)
>  			/* Physical mode. */
> -			result = kvm_apic_match_physical_addr(target, dest);
> +			return kvm_apic_match_physical_addr(target, dest);
>  		else
>  			/* Logical mode. */
> -			result = kvm_apic_match_logical_addr(target, dest);
> -		break;
> +			return kvm_apic_match_logical_addr(target, dest);
>  	case APIC_DEST_SELF:
> -		result = (target == source);
> -		break;
> +		return target == source;
>  	case APIC_DEST_ALLINC:
> -		result = 1;
> -		break;
> +		return true;
>  	case APIC_DEST_ALLBUT:
> -		result = (target != source);
> -		break;
> +		return target != source;
>  	default:
>  		apic_debug("kvm: apic: Bad dest shorthand value %x\n",
>  			   short_hand);
> -		break;
> +		return false;
>  	}
> -
> -	return result;
>  }
>  
>  bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
> 
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Radim Krčmář Jan. 30, 2015, 1:06 p.m. UTC | #2
2015-01-30 09:52+0100, Paolo Bonzini:
> On 29/01/2015 22:48, Radim Kr?má? wrote:
> > The majority of this patch turns
> >   result = 0; if (CODE) result = 1; return result;
> > into
> >   return CODE;
> > because we return bool now.
> 
> Added a bunch of "!= 0" to avoid automagic conversion to bool.

Makes sense, (valid code isn't automatically desirable)
thank you.
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Radim Krčmář Feb. 2, 2015, 2:26 p.m. UTC | #3
2015-01-30 09:52+0100, Paolo Bonzini:
> On 29/01/2015 22:48, Radim Kr?má? wrote:
> > The majority of this patch turns
> >   result = 0; if (CODE) result = 1; return result;
> > into
> >   return CODE;
> > because we return bool now.
> 
> Added a bunch of "!= 0" to avoid automagic conversion to bool.

(I haven't checked it earlier ... sorry.)

> > -		if (((logical_id >> 4) == (mda >> 0x4))
> > -		    && (logical_id & mda & 0xf))
> > -			result = 1;
> > -		break;
> > +		return ((logical_id >> 4) == (mda >> 4))
> > +		       && (logical_id & mda & 0xf);

was merged as

+		return ((logical_id >> 4) == (mda >> 4))
+		       && (logical_id & mda & 0xf) != 0;

but it has to be parenthesized ('&&' has lower precedence than '!=').
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Paolo Bonzini Feb. 2, 2015, 2:28 p.m. UTC | #4
On 02/02/2015 15:26, Radim Kr?má? wrote:
>>> > > +		return ((logical_id >> 4) == (mda >> 4))
>>> > > +		       && (logical_id & mda & 0xf);
> was merged as
> 
> +		return ((logical_id >> 4) == (mda >> 4))
> +		       && (logical_id & mda & 0xf) != 0;
> 
> but it has to be parenthesized ('&&' has lower precedence than '!=').

Lower precedence means that the merged version is right (unless my brain
went bonkers, which I cannot exclude).  "!=" has higher precedence and
thus it is implicitly parenthesized.

In fact the first comparison could have its parentheses removed as well.

Paolo
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Radim Krčmář Feb. 2, 2015, 2:29 p.m. UTC | #5
2015-02-02 15:26+0100, Radim Kr?má?:
> 2015-01-30 09:52+0100, Paolo Bonzini:
> +		return ((logical_id >> 4) == (mda >> 4))
> +		       && (logical_id & mda & 0xf) != 0;
> 
> but it has to be parenthesized ('&&' has lower precedence than '!=').

No, my bad, I understood it now.
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Radim Krčmář Feb. 2, 2015, 2:30 p.m. UTC | #6
2015-02-02 15:28+0100, Paolo Bonzini:
> 
> 
> On 02/02/2015 15:26, Radim Kr?má? wrote:
> >>> > > +		return ((logical_id >> 4) == (mda >> 4))
> >>> > > +		       && (logical_id & mda & 0xf);
> > was merged as
> > 
> > +		return ((logical_id >> 4) == (mda >> 4))
> > +		       && (logical_id & mda & 0xf) != 0;
> > 
> > but it has to be parenthesized ('&&' has lower precedence than '!=').
> 
> Lower precedence means that the merged version is right (unless my brain
> went bonkers, which I cannot exclude).  "!=" has higher precedence and
> thus it is implicitly parenthesized.
> 
> In fact the first comparison could have its parentheses removed as well.

Yes, it could be,
  logical_id >> 4 == mda >> 4 && (logical_id & mda & 0xf) != 0

I missed the point and thought that you wanted to turn the whole
expression into bool, when it already was one.
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index 478dd8bd653b..271e7d076879 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -594,42 +594,34 @@  static bool kvm_apic_match_physical_addr(struct kvm_lapic *apic, u32 dest)
 
 static bool kvm_apic_match_logical_addr(struct kvm_lapic *apic, u32 mda)
 {
-	int result = 0;
 	u32 logical_id;
 
 	if (kvm_apic_broadcast(apic, mda))
-		return 1;
+		return true;
 
-	if (apic_x2apic_mode(apic)) {
-		logical_id = kvm_apic_get_reg(apic, APIC_LDR);
+	logical_id = kvm_apic_get_reg(apic, APIC_LDR);
+
+	if (apic_x2apic_mode(apic))
 		return logical_id & mda;
-	}
 
-	logical_id = GET_APIC_LOGICAL_ID(kvm_apic_get_reg(apic, APIC_LDR));
+	logical_id = GET_APIC_LOGICAL_ID(logical_id);
 
 	switch (kvm_apic_get_reg(apic, APIC_DFR)) {
 	case APIC_DFR_FLAT:
-		if (logical_id & mda)
-			result = 1;
-		break;
+		return logical_id & mda;
 	case APIC_DFR_CLUSTER:
-		if (((logical_id >> 4) == (mda >> 0x4))
-		    && (logical_id & mda & 0xf))
-			result = 1;
-		break;
+		return ((logical_id >> 4) == (mda >> 4))
+		       && (logical_id & mda & 0xf);
 	default:
 		apic_debug("Bad DFR vcpu %d: %08x\n",
 			   apic->vcpu->vcpu_id, kvm_apic_get_reg(apic, APIC_DFR));
-		break;
+		return false;
 	}
-
-	return result;
 }
 
 bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
 			   int short_hand, unsigned int dest, int dest_mode)
 {
-	int result = 0;
 	struct kvm_lapic *target = vcpu->arch.apic;
 
 	apic_debug("target %p, source %p, dest 0x%x, "
@@ -641,27 +633,21 @@  bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
 	case APIC_DEST_NOSHORT:
 		if (dest_mode == 0)
 			/* Physical mode. */
-			result = kvm_apic_match_physical_addr(target, dest);
+			return kvm_apic_match_physical_addr(target, dest);
 		else
 			/* Logical mode. */
-			result = kvm_apic_match_logical_addr(target, dest);
-		break;
+			return kvm_apic_match_logical_addr(target, dest);
 	case APIC_DEST_SELF:
-		result = (target == source);
-		break;
+		return target == source;
 	case APIC_DEST_ALLINC:
-		result = 1;
-		break;
+		return true;
 	case APIC_DEST_ALLBUT:
-		result = (target != source);
-		break;
+		return target != source;
 	default:
 		apic_debug("kvm: apic: Bad dest shorthand value %x\n",
 			   short_hand);
-		break;
+		return false;
 	}
-
-	return result;
 }
 
 bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,