Skip to content
  • Michal Nazarewicz's avatar
    kernel.h: make abs() work with 64-bit types · c8299cb6
    Michal Nazarewicz authored
    For 64-bit arguments, the abs macro casts it to an int which leads to
    lost precision and may cause incorrect results.  To deal with 64-bit
    types abs64 macro has been introduced but still there are places where
    abs macro is used incorrectly.
    
    To deal with the problem, expand abs macro such that it operates on s64
    type when dealing with 64-bit types while still returning long when
    dealing with smaller types.
    
    This fixes one known bug (per John):
    
    The internal clocksteering done for fine-grained error correction uses a
    : logarithmic approximation, so any time adjtimex() adjusts the clock
    : steering, timekeeping_freqadjust() quickly approximates the correct clock
    : frequency over a series of ticks.
    :
    : Unfortunately, the logic in timekeeping_freqadjust(), introduced in commit
    : dc491596
    
     (Rework frequency adjustments to work better w/ nohz),
    : used the abs() function with a s64 error value to calculate the size of
    : the approximated adjustment to be made.
    :
    : Per include/linux/kernel.h: "abs() should not be used for 64-bit types
    : (s64, u64, long long) - use abs64()".
    :
    : Thus on 32-bit platforms, this resulted in the clocksteering to take a
    : quite dampended random walk trying to converge on the proper frequency,
    : which caused the adjustments to be made much slower then intended (most
    : easily observed when large adjustments are made).
    
    Signed-off-by: default avatarMichal Nazarewicz <mina86@mina86.com>
    Reported-by: default avatarJohn Stultz <john.stultz@linaro.org>
    Tested-by: default avatarJohn Stultz <john.stultz@linaro.org>
    Cc: Ingo Molnar <mingo@kernel.org>
    Cc: Steven Rostedt <rostedt@goodmis.org>
    Cc: Peter Zijlstra <peterz@infradead.org>
    Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
    Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
    c8299cb6