[RFC,1/7] timekeeping: Fix cross-timestamp interpolation on counter wrap

Message ID 20230630171052.985577-2-peter.hilber@opensynergy.com
State New
Headers
Series [RFC,1/7] timekeeping: Fix cross-timestamp interpolation on counter wrap |

Commit Message

Peter Hilber June 30, 2023, 5:10 p.m. UTC
  cycle_between() decides whether get_device_system_crosststamp() will
interpolate for older counter readings.

cycle_between() yields wrong results for a counter wrap-around where after
< before < test, and for the case after < test < before.

Fix the comparison logic.

Fixes: 2c756feb18d9 ("time: Add history to cross timestamp interface supporting slower devices")
Signed-off-by: Peter Hilber <peter.hilber@opensynergy.com>
---
 kernel/time/timekeeping.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

John Stultz July 7, 2023, 10:51 p.m. UTC | #1
On Fri, Jun 30, 2023 at 10:12 AM Peter Hilber
<peter.hilber@opensynergy.com> wrote:
>
> cycle_between() decides whether get_device_system_crosststamp() will
> interpolate for older counter readings.
>
> cycle_between() yields wrong results for a counter wrap-around where after
> < before < test, and for the case after < test < before.
>
> Fix the comparison logic.
>
> Fixes: 2c756feb18d9 ("time: Add history to cross timestamp interface supporting slower devices")
> Signed-off-by: Peter Hilber <peter.hilber@opensynergy.com>
> ---
>  kernel/time/timekeeping.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
> index 266d02809dbb..8f35455b6250 100644
> --- a/kernel/time/timekeeping.c
> +++ b/kernel/time/timekeeping.c
> @@ -1186,7 +1186,7 @@ static bool cycle_between(u64 before, u64 test, u64 after)
>  {
>         if (test > before && test < after)
>                 return true;
> -       if (test < before && before > after)
> +       if (before > after && (test > before || test < after))
>                 return true;
>         return false;
>  }

Thanks for catching this and sending it in.
Looks good to me. Curious: Did you actually hit such a wrap around with u64s?

Acked-by: John Stultz <jstultz@google.com>

thanks
-john
  
Peter Hilber July 27, 2023, 10:21 a.m. UTC | #2
On 08.07.23 00:51, John Stultz wrote:
> On Fri, Jun 30, 2023 at 10:12 AM Peter Hilber
> <peter.hilber@opensynergy.com> wrote:
>>
>> cycle_between() decides whether get_device_system_crosststamp() will
>> interpolate for older counter readings.
>>
>> cycle_between() yields wrong results for a counter wrap-around where after
>> < before < test, and for the case after < test < before.
>>
>> Fix the comparison logic.
>>
>> Fixes: 2c756feb18d9 ("time: Add history to cross timestamp interface supporting slower devices")
>> Signed-off-by: Peter Hilber <peter.hilber@opensynergy.com>
>> ---
>>  kernel/time/timekeeping.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
>> index 266d02809dbb..8f35455b6250 100644
>> --- a/kernel/time/timekeeping.c
>> +++ b/kernel/time/timekeeping.c
>> @@ -1186,7 +1186,7 @@ static bool cycle_between(u64 before, u64 test, u64 after)
>>  {
>>         if (test > before && test < after)
>>                 return true;
>> -       if (test < before && before > after)
>> +       if (before > after && (test > before || test < after))
>>                 return true;
>>         return false;
>>  }
> 
> Thanks for catching this and sending it in.
> Looks good to me. Curious: Did you actually hit such a wrap around with u64s?

No, I just saw this when fixing the bug in the next patch.

Thanks,

Peter
  

Patch

diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index 266d02809dbb..8f35455b6250 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -1186,7 +1186,7 @@  static bool cycle_between(u64 before, u64 test, u64 after)
 {
 	if (test > before && test < after)
 		return true;
-	if (test < before && before > after)
+	if (before > after && (test > before || test < after))
 		return true;
 	return false;
 }