2018-10-15 17:17:52

by H. Peter Anvin

[permalink] [raw]
Subject: Re: [PATCH] kvm/x86 : avoid shifting signed 32-bit value by 31 bits

On 10/7/18 6:04 PM, [email protected] wrote:
\>>>>>
>>>>> #define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF)
>>>>> -#define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31)
>>>>> +#define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1UL << 31)
>>>
>>>> It is reasonable to change to unsigned, while not necessary to unsigned
>>>> long?
>>> AVIC_LOGICAL_ID_ENTRY_VALID_MASK is used in function avic_ldr_write.
>>> here I think it doesn't matter if you use unsigned or unsigned long. Do you have any suggestions?
>
>> In current case, AVIC_LOGICAL_ID_ENTRY_VALID_MASK is used to calculate
>> the value of new_entry with type of u32. So the definition here is not
>> harmful.
>
>> Also, I did a quick grep and found similar definition (1 << 31) is popular
>> in the whole kernel tree.
>
>> The reason to make this change is not that strong to me. Would you
>> minding sharing more reason behind this change?
> oh, I'm just thinking logically, not more reason.

The right way to do this would be to use the _BITUL() (or _BITULL()) macro.

-hpa



2018-10-15 17:24:31

by Paolo Bonzini

[permalink] [raw]
Subject: Re: [PATCH] kvm/x86 : avoid shifting signed 32-bit value by 31 bits

On 15/10/2018 19:16, H. Peter Anvin wrote:
> On 10/7/18 6:04 PM, [email protected] wrote:
> \>>>>>
>>>>>> #define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF)
>>>>>> -#define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31)
>>>>>> +#define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1UL << 31)
>>>>
>>>>> It is reasonable to change to unsigned, while not necessary to unsigned
>>>>> long?
>>>> AVIC_LOGICAL_ID_ENTRY_VALID_MASK is used in function avic_ldr_write.
>>>> here I think it doesn't matter if you use unsigned or unsigned long. Do you have any suggestions?
>>
>>> In current case, AVIC_LOGICAL_ID_ENTRY_VALID_MASK is used to calculate
>>> the value of new_entry with type of u32. So the definition here is not
>>> harmful.
>>
>>> Also, I did a quick grep and found similar definition (1 << 31) is popular
>>> in the whole kernel tree.
>>
>>> The reason to make this change is not that strong to me. Would you
>>> minding sharing more reason behind this change?
>> oh, I'm just thinking logically, not more reason.
>
> The right way to do this would be to use the _BITUL() (or _BITULL()) macro.

Even for a value from a 32-bit register? That would be _BIT, which
doesn't exist.

Paolo

2018-10-15 17:31:49

by H. Peter Anvin

[permalink] [raw]
Subject: Re: [PATCH] kvm/x86 : avoid shifting signed 32-bit value by 31 bits

On 10/15/18 10:23 AM, Paolo Bonzini wrote:
>
> Even for a value from a 32-bit register? That would be _BIT, which
> doesn't exist.
>

Just use _BITUL(). gcc is smart enough to know that that the resulting value
is representable in 32 bits.

Or if you really care, submit a patch to create _BITU(), but I don't
personally see much of a point.

-hpa