Skip to contentSkip to conversation

time: Fix timekeeping_freqadjust()'s incorrect use of abs() instead of abs64()

RFC PATCH In review John Stultz 2 participants 4 messages Sep 9, 2015

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/2 time: Fix timekeeping_freqadjust()'s incorrect use of abs() instead of abs64()

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 dc491596f639 ("timekeeping: 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).

This patch fixes the issue by using abs64() instead.

Cc Nuno Gonçalves <[email protected]>
Cc Miroslav Lichvar <[email protected]>
Cc Prarit Bhargava <[email protected]>
Cc Richard Cochran <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc <[email protected]> # v3.17+
---
 kernel/time/timekeeping.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/timekeeping.c +1 -1
index f6ee2e6..3739ac6 100644
@@ -1614,7 +1614,7 @@ static __always_inline void timekeeping_freqadjust(struct timekeeper *tk,
 	negative = (tick_error < 0);
 
 	/* Sort out the magnitude of the correction */
-	tick_error = abs(tick_error);
+	tick_error = abs64(tick_error);
 	for (adj = 0; tick_error > interval; adj++)
 		tick_error >>= 1;
 
Signature · 1 line
1.9.1
#1 lore raw

RFC PATCH 2/2 kselftest: timers: Add adjtick test to validate adjtimex() tick adjustments

Recently an issue was reported that was difficult to detect except by tweaking the adjtimex tick value, and noticing how quickly the adjustment took to be made:

	https://lkml.org/lkml/2015/9/1/488

Thus this patch introduces a new test which manipulates the adjtimex tick value and validates the results are what we expect.

Cc Nuno Gonçalves <[email protected]>
Cc Miroslav Lichvar <[email protected]>
Cc Prarit Bhargava <[email protected]>
Cc Richard Cochran <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Shuah Khan <[email protected]>
---
 tools/testing/selftests/timers/Makefile  |   3 +-
 tools/testing/selftests/timers/adjtick.c | 186 +++++++++++++++++++++++++++++++
 2 files changed, 188 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/timers/adjtick.c
tools/testing/selftests/timers/Makefile +2 -1
index 89a3f44..4a1be1b 100644
@@ -8,7 +8,7 @@ LDFLAGS += -lrt -lpthread
 TEST_PROGS = posix_timers nanosleep nsleep-lat set-timer-lat mqueue-lat \
 	     inconsistency-check raw_skew threadtest rtctest
 
-TEST_PROGS_EXTENDED = alarmtimer-suspend valid-adjtimex change_skew \
+TEST_PROGS_EXTENDED = alarmtimer-suspend valid-adjtimex adjtick change_skew \
 		      skew_consistency clocksource-switch leap-a-day \
 		      leapcrash set-tai set-2038
 
@@ -24,6 +24,7 @@ include ../lib.mk
 run_destructive_tests: run_tests
 	./alarmtimer-suspend
 	./valid-adjtimex
+	./adjtick
 	./change_skew
 	./skew_consistency
 	./clocksource-switch
tools/testing/selftests/timers/adjtick.c new file +186 -0

186 lines not on this page · raw message

Signature · 1 line
1.9.1
#2 lore raw

Re: [PATCH 2/2][RFC] kselftest: timers: Add adjtick test to validate adjtimex() tick adjustments

Quoted 7 lines · John Stultz

Recently an issue was reported that was difficult to detect except by tweaking the adjtimex tick value, and noticing how quickly the adjustment took to be made:

	https://lkml.org/lkml/2015/9/1/488

Thus this patch introduces a new test which manipulates the adjtimex tick value and validates the results are what we expect.

Great!

tools/testing/selftests/timers/adjtick.c line 133

Quoted 1 line
tools/testing/selftests/timers/adjtick.c new file +1 -0
+	ppm = ((long long)tickval * MILLION)/10000 - MILLION;

I think this needs to be based on sysconf(_SC_CLK_TCK) or similar, since the user-space HZ is not 100 on all archs.

tools/testing/selftests/timers/adjtick.c line 169

Quoted 2 lines
tools/testing/selftests/timers/adjtick.c new file +2 -0
+	for (tick = 9000; tick < 11000; tick += 250)
+		if (check_tick_adj(tick)) {

This too.

tools/testing/selftests/timers/adjtick.c line 181

Quoted 2 lines
tools/testing/selftests/timers/adjtick.c new file +2 -0
+	tx1.tick = 10000;
+	adjtimex(&tx1);

And this too.

Signature · 1 line
Miroslav Lichvar
#3 lore raw

Re: [PATCH 2/2][RFC] kselftest: timers: Add adjtick test to validate adjtimex() tick adjustments

Quoted 9 lines · Miroslav Lichvar
Quoted 7 lines · John Stultz

Recently an issue was reported that was difficult to detect except by tweaking the adjtimex tick value, and noticing how quickly the adjustment took to be made:

      https://lkml.org/lkml/2015/9/1/488

Thus this patch introduces a new test which manipulates the adjtimex tick value and validates the results are what we expect.

Quoted 6 lines

Great!

Quoted 1 line

+ ppm = ((long long)tickval * MILLION)/10000 - MILLION;

I think this needs to be based on sysconf(_SC_CLK_TCK) or similar, since the user-space HZ is not 100 on all archs.

Good point. Fixing that up now. Will resend in a bit.

thanks
-john