lkml.org 
[lkml]   [2015]   [May]   [6]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
SubjectRe: [RFC][PATCH v2] ktime: Fix ktime_divns to do signed division
From
On Wed, May 6, 2015 at 1:11 PM, Nicolas Pitre <nicolas.pitre@linaro.org> wrote:
> On Wed, 6 May 2015, John Stultz wrote:
>
>> It was noted that the 32bit implementation of ktime_divns()
>> was doing unsigned division and didn't properly handle
>> negative values.
>>
>> This patch fixes the problem by checking and preserving
>> the sign bit, and then reapplying it if appropriate after
>> the division, it also changes the return type to a s64
>> to make it more obvious this is expected.
>>
> [...]
>>
>> -# define ktime_divns(kt, div) (u64)((kt).tv64 / (div))
>> +static inline s64 ktime_divns(const ktime_t kt, s64 div)
>> +{
>> + /*
>> + * 32-bit implementation cannot handle negative divisors,
>> + * so catch them on 64bit as well.
>> + */
>> + BUG_ON(div < 0);
>> + return (u64)((kt).tv64 / (div))
>
> You forgot to remove the u64 cast?

Thanks for catching that!

> Is there a reason why it was there originally?

I assume because nanoseconds are usually though of as unsigned. It was
introduced way back in the original hrtimer core code:
c0a3132963db68f1fbbd0e316b73de100fee3f08

thanks
-john


\
 
 \ /
  Last update: 2015-05-07 00:21    [W:0.068 / U:1.288 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site