[PATCH] x86/vmware: use unsigned integer for shifting

Shreenidhi Shedi posted 1 patch 3 years, 11 months ago
There is a newer version of this series
arch/x86/kernel/cpu/vmware.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] x86/vmware: use unsigned integer for shifting
Posted by Shreenidhi Shedi 3 years, 11 months ago
Shifting signed 32-bit value by 31 bits is implementation-defined
behaviour. Using unsigned is better option for this.

Signed-off-by: Shreenidhi Shedi <sshedi@vmware.com>
---
 arch/x86/kernel/cpu/vmware.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/kernel/cpu/vmware.c b/arch/x86/kernel/cpu/vmware.c
index c04b933f48d3..b28f789d3c56 100644
--- a/arch/x86/kernel/cpu/vmware.c
+++ b/arch/x86/kernel/cpu/vmware.c
@@ -476,7 +476,7 @@ static bool __init vmware_legacy_x2apic_available(void)
 {
 	uint32_t eax, ebx, ecx, edx;
 	VMWARE_CMD(GETVCPU_INFO, eax, ebx, ecx, edx);
-	return (eax & (1 << VMWARE_CMD_VCPU_RESERVED)) == 0 &&
+	return (eax & (1U << VMWARE_CMD_VCPU_RESERVED)) == 0 &&
 	       (eax & (1 << VMWARE_CMD_LEGACY_X2APIC)) != 0;
 }
 
-- 
2.36.1
Re: [PATCH] x86/vmware: use unsigned integer for shifting
Posted by Borislav Petkov 3 years, 11 months ago
On Fri, May 20, 2022 at 12:58:57PM +0530, Shreenidhi Shedi wrote:
> Shifting signed 32-bit value by 31 bits is implementation-defined
> behaviour. Using unsigned is better option for this.
> 
> Signed-off-by: Shreenidhi Shedi <sshedi@vmware.com>
> ---
>  arch/x86/kernel/cpu/vmware.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kernel/cpu/vmware.c b/arch/x86/kernel/cpu/vmware.c
> index c04b933f48d3..b28f789d3c56 100644
> --- a/arch/x86/kernel/cpu/vmware.c
> +++ b/arch/x86/kernel/cpu/vmware.c
> @@ -476,7 +476,7 @@ static bool __init vmware_legacy_x2apic_available(void)
>  {
>  	uint32_t eax, ebx, ecx, edx;
>  	VMWARE_CMD(GETVCPU_INFO, eax, ebx, ecx, edx);
> -	return (eax & (1 << VMWARE_CMD_VCPU_RESERVED)) == 0 &&
> +	return (eax & (1U << VMWARE_CMD_VCPU_RESERVED)) == 0 &&
>  	       (eax & (1 << VMWARE_CMD_LEGACY_X2APIC)) != 0;

Or you can use the BIT() macro and simplify this expression even more:

       return !(eax & BIT(VMWARE_CMD_VCPU_RESERVED)) &&
               (eax & BIT(VMWARE_CMD_LEGACY_X2APIC));


-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
Re: [PATCH] x86/vmware: use unsigned integer for shifting
Posted by Srivatsa S. Bhat 3 years, 11 months ago
Hi Shreenidhi,

Thank you for the patch!

On 5/20/22 2:26 AM, Borislav Petkov wrote:
> On Fri, May 20, 2022 at 12:58:57PM +0530, Shreenidhi Shedi wrote:
>> Shifting signed 32-bit value by 31 bits is implementation-defined
>> behaviour. Using unsigned is better option for this.
>>

Can you also add a "Fixes:" tag with the commit that introduced the
issue? I believe it is 4cca6ea04d31 ("x86/apic: Allow x2apic without
IR on VMware platform").

>> Signed-off-by: Shreenidhi Shedi <sshedi@vmware.com>
>> ---
>>  arch/x86/kernel/cpu/vmware.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/arch/x86/kernel/cpu/vmware.c b/arch/x86/kernel/cpu/vmware.c
>> index c04b933f48d3..b28f789d3c56 100644
>> --- a/arch/x86/kernel/cpu/vmware.c
>> +++ b/arch/x86/kernel/cpu/vmware.c
>> @@ -476,7 +476,7 @@ static bool __init vmware_legacy_x2apic_available(void)
>>  {
>>  	uint32_t eax, ebx, ecx, edx;
>>  	VMWARE_CMD(GETVCPU_INFO, eax, ebx, ecx, edx);
>> -	return (eax & (1 << VMWARE_CMD_VCPU_RESERVED)) == 0 &&
>> +	return (eax & (1U << VMWARE_CMD_VCPU_RESERVED)) == 0 &&
>>  	       (eax & (1 << VMWARE_CMD_LEGACY_X2APIC)) != 0;
> 
> Or you can use the BIT() macro and simplify this expression even more:
> 
>        return !(eax & BIT(VMWARE_CMD_VCPU_RESERVED)) &&
>                (eax & BIT(VMWARE_CMD_LEGACY_X2APIC));
> 
> 

That's better indeed.

Regards,
Srivatsa
Re: [PATCH] x86/vmware: use unsigned integer for shifting
Posted by Shreenidhi Shedi 3 years, 11 months ago
On 20/05/22 4:59 pm, Srivatsa S. Bhat wrote:
> Hi Shreenidhi,
> 
> Thank you for the patch!
> 
> On 5/20/22 2:26 AM, Borislav Petkov wrote:
>> On Fri, May 20, 2022 at 12:58:57PM +0530, Shreenidhi Shedi wrote:
>>> Shifting signed 32-bit value by 31 bits is implementation-defined
>>> behaviour. Using unsigned is better option for this.
>>>
> 
> Can you also add a "Fixes:" tag with the commit that introduced the
> issue? I believe it is 4cca6ea04d31 ("x86/apic: Allow x2apic without
> IR on VMware platform").
> 
>>> Signed-off-by: Shreenidhi Shedi <sshedi@vmware.com>
>>> ---
>>>  arch/x86/kernel/cpu/vmware.c | 2 +-
>>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>>
>>> diff --git a/arch/x86/kernel/cpu/vmware.c b/arch/x86/kernel/cpu/vmware.c
>>> index c04b933f48d3..b28f789d3c56 100644
>>> --- a/arch/x86/kernel/cpu/vmware.c
>>> +++ b/arch/x86/kernel/cpu/vmware.c
>>> @@ -476,7 +476,7 @@ static bool __init vmware_legacy_x2apic_available(void)
>>>  {
>>>  	uint32_t eax, ebx, ecx, edx;
>>>  	VMWARE_CMD(GETVCPU_INFO, eax, ebx, ecx, edx);
>>> -	return (eax & (1 << VMWARE_CMD_VCPU_RESERVED)) == 0 &&
>>> +	return (eax & (1U << VMWARE_CMD_VCPU_RESERVED)) == 0 &&
>>>  	       (eax & (1 << VMWARE_CMD_LEGACY_X2APIC)) != 0;
>>
>> Or you can use the BIT() macro and simplify this expression even more:
>>
>>        return !(eax & BIT(VMWARE_CMD_VCPU_RESERVED)) &&
>>                (eax & BIT(VMWARE_CMD_LEGACY_X2APIC));
>>
>>
> 
> That's better indeed.
> 
> Regards,
> Srivatsa


Thanks for the quick reviews Boris and Srivatsa. I will make the changes and push the another patch soon.

--
Shedi