Time related fixes for 3.17/stable
Subsystems (2)
cover Time related fixes for 3.17/stable
Hey Thomas, Ingo,
Here are a few fixes for 3.17/stable that came in over the last week or so.
The first is a fix to the timeval to jiffies conversion that was incorrectly rounding up by one even when the value divided precisely. This issue is apparently quite long standing, but shows up as in increasing timeout when folks re-use the returned old_value from setitimer.
The second group are yet further fixes to the alarmtimer code. This time catching incorrect timer_gettime semantics as well as fixes for improper signaling and locking. These all go back to 3.0, and the timer_gettime change could cause problems with existing apps, but I suspect that aspect of the interface hasn't been used much until recently.
These are against tip/timers/urgent, and have run successfully through my timekeeping tests for x86_64, i386 and arm.
Let me know if you have any comments or objections.
thanks
-john
Pull request below if you'd prefer to take it that way:
The following changes since commit 9bf2419fa7bffa16ce58a4d5c20399eff8c970c9:
timekeeping: Update timekeeper before updating vsyscall and pvclock (2014-09-06 12:58:18 +0200)
are available in the git repository at:
https://git.linaro.org/people/john.stultz/linux.git fortglx/3.17/time
for you to fetch changes up to 474e941bed9262f5fa2394f9a4a67e24499e5926:
alarmtimer: Lock k_itimer during timer callback (2014-09-12 13:59:12 -0700)
----------------------------------------------------------------
Andrew Hunter (1):
jiffies: Fix timeval conversion to jiffies
Richard Larocque (3):
alarmtimer: Return relative times in timer_gettime
alarmtimer: Do not signal SIGEV_NONE timers
alarmtimer: Lock k_itimer during timer callback
include/linux/jiffies.h | 12 ------------ kernel/time/alarmtimer.c | 34 +++++++++++++++++++++++----------- kernel/time/time.c | 56 +++++++++++++++++++++++++++++++------------------------- 3 files changed, 54 insertions(+), 48 deletions(-)
Andrew Hunter (1):
jiffies: Fix timeval conversion to jiffies
Richard Larocque (3):
alarmtimer: Return relative times in timer_gettime
alarmtimer: Do not signal SIGEV_NONE timers
alarmtimer: Lock k_itimer during timer callback
include/linux/jiffies.h | 12 ----------- kernel/time/alarmtimer.c | 34 +++++++++++++++++++---------- kernel/time/time.c | 56 +++++++++++++++++++++++++++--------------------- 3 files changed, 54 insertions(+), 48 deletions(-)
Signature · 1 line
1.9.1
PATCH 1/4 jiffies: Fix timeval conversion to jiffies
From: Andrew Hunter <[email protected]>
timeval_to_jiffies tried to round a timeval up to an integral number of jiffies, but the logic for doing so was incorrect: intervals corresponding to exactly N jiffies would become N+1. This manifested itself particularly repeatedly stopping/starting an itimer:
setitimer(ITIMER_PROF, &val, NULL);setitimer(ITIMER_PROF, NULL, &val);
would add a full tick to val, _even if it was exactly representable in terms of jiffies_ (say, the result of a previous rounding.) Doing this repeatedly would cause unbounded growth in val. So fix the math.
Here's what was wrong with the conversion: we essentially computed (eliding seconds)
jiffies = usec * (NSEC_PER_USEC/TICK_NSEC)
by using scaling arithmetic, which took the best approximation of NSEC_PER_USEC/TICK_NSEC with denominator of 2^USEC_JIFFIE_SC = x/(2^USEC_JIFFIE_SC), and computed:
jiffies = (usec * x) >> USEC_JIFFIE_SC
and rounded this calculation up in the intermediate form (since we can't necessarily exactly represent TICK_NSEC in usec.) But the scaling arithmetic is a (very slight) *over*approximation of the true value; that is, instead of dividing by (1 usec/ 1 jiffie), we effectively divided by (1 usec/1 jiffie)-epsilon (rounding down). This would normally be fine, but we want to round timeouts up, and we did so by adding 2^USEC_JIFFIE_SC - 1 before the shift; this would be fine if our division was exact, but dividing this by the slightly smaller factor was equivalent to adding just _over_ 1 to the final result (instead of just _under_ 1, as desired.)
In particular, with HZ=1000, we consistently computed that 10000 usec was 11 jiffies; the same was true for any exact multiple of TICK_NSEC.
We could possibly still round in the intermediate form, adding something less than 2^USEC_JIFFIE_SC - 1, but easier still is to convert usec->nsec, round in nanoseconds, and then convert using time*spec*_to_jiffies. This adds one constant multiplication, and is not observably slower in microbenchmarks on recent x86 hardware.
Tested: the following program:
int main() {
struct itimerval zero = {{0, 0}, {0, 0}};
/* Initially set to 10 ms. */
struct itimerval initial = zero;
initial.it_interval.tv_usec = 10000;
setitimer(ITIMER_PROF, &initial, NULL);
/* Save and restore several times. */
for (size_t i = 0; i < 10; ++i) {
struct itimerval prev;
setitimer(ITIMER_PROF, &zero, &prev);
/* on old kernels, this goes up by TICK_USEC every iteration */
printf("previous value: %ld %ld %ld %ld\n",
prev.it_interval.tv_sec, prev.it_interval.tv_usec,
prev.it_value.tv_sec, prev.it_value.tv_usec);
setitimer(ITIMER_PROF, &prev, NULL);
}
return 0;
}--- include/linux/jiffies.h | 12 ----------- kernel/time/time.c | 56 +++++++++++++++++++++++++++---------------------- 2 files changed, 31 insertions(+), 37 deletions(-)
include/linux/jiffies.h +0 -12
@@ -258,23 +258,11 @@ extern unsigned long preset_lpj;#define SEC_JIFFIE_SC (32 - SHIFT_HZ)#endif #define NSEC_JIFFIE_SC (SEC_JIFFIE_SC + 29) -#define USEC_JIFFIE_SC (SEC_JIFFIE_SC + 19)#define SEC_CONVERSION ((unsigned long)((((u64)NSEC_PER_SEC << SEC_JIFFIE_SC) +\ TICK_NSEC -1) / (u64)TICK_NSEC))#define NSEC_CONVERSION ((unsigned long)((((u64)1 << NSEC_JIFFIE_SC) +\ TICK_NSEC -1) / (u64)TICK_NSEC)) -#define USEC_CONVERSION \ - ((unsigned long)((((u64)NSEC_PER_USEC << USEC_JIFFIE_SC) +\ - TICK_NSEC -1) / (u64)TICK_NSEC)) -/* -* USEC_ROUND is used in the timeval to jiffie conversion. See there -* for more details. It is the scaled resolution rounding value. Note -* that it is a 64-bit value. Since, when it is applied, we are already -* in jiffies (albit scaled), it is nothing but the bits we will shift -* off. -*/ -#define USEC_ROUND (u64)(((u64)1 << USEC_JIFFIE_SC) - 1)/* * The maximum jiffie value is (MAX_INT >> 1). Here we translate that * into seconds. The 64-bit case will overflow if we are not careful,
kernel/time/time.c +31 -25
85 lines not on this page · raw message
Signature · 1 line
1.9.1
PATCH 2/4 alarmtimer: Return relative times in timer_gettime
In reply to John StultzFrom: Richard Larocque <[email protected]>
Returns the time remaining for an alarm timer, rather than the time at which it is scheduled to expire. If the timer has already expired or it is not currently scheduled, the it_value's members are set to zero.
This new behavior matches that of the other posix-timers and the POSIX specifications.
This is a change in user-visible behavior, and may break existing applications. Hopefully, few users rely on the old incorrect behavior.
--- kernel/time/alarmtimer.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)
kernel/time/alarmtimer.c +11 -7
@@ -541,18 +541,22 @@ static int alarm_timer_create(struct k_itimer *new_timer)* @new_timer: k_itimer pointer * @cur_setting: itimerspec data to fill * -* Copies the itimerspec data out from the k_itimer +* Copies out the current itimerspec data */ static void alarm_timer_get (struct k_itimer *timr,struct itimerspec *cur_setting) { -memset (cur_setting,0 ,sizeof (struct itimerspec )); +ktime_t relative_expiry_time = +alarm_expires_remaining (&(timr->it.alarm.alarmtimer)); - cur_setting->it_interval = -ktime_to_timespec (timr->it.alarm.interval); - cur_setting->it_value = -ktime_to_timespec (timr->it.alarm.alarmtimer.node.expires); -return ; +if (ktime_to_ns (relative_expiry_time) >0 ) { + cur_setting->it_value =ktime_to_timespec (relative_expiry_time); + }else { + cur_setting->it_value.tv_sec =0 ; + cur_setting->it_value.tv_nsec =0 ; + } + + cur_setting->it_interval =ktime_to_timespec (timr->it.alarm.interval); }/**
Signature · 1 line
1.9.1
PATCH 3/4 alarmtimer: Do not signal SIGEV_NONE timers
In reply to John StultzFrom: Richard Larocque <[email protected]>
Avoids sending a signal to alarm timers created with sigev_notify set to SIGEV_NONE by checking for that special case in the timeout callback.
The regular posix timers avoid sending signals to SIGEV_NONE timers by not scheduling any callbacks for them in the first place. Although it would be possible to do something similar for alarm timers, it's simpler to handle this as a special case in the timeout.
Prior to this patch, the alarm timer would ignore the sigev_notify value and try to deliver signals to the process anyway. Even worse, the sanity check for the value of sigev_signo is skipped when SIGEV_NONE was specified, so the signal number could be bogus. If sigev_signo was an unitialized value (as it often would be if SIGEV_NONE is used), then it's hard to predict which signal will be sent.
--- kernel/time/alarmtimer.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/time/alarmtimer.c +4 -2
@@ -466,8 +466,10 @@ static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm, { struct k_itimer *ptr = container_of(alarm, struct k_itimer, it.alarm.alarmtimer); -if (posix_timer_event (ptr,0 ) !=0 ) - ptr->it_overrun++; +if ((ptr->it_sigev_notify & ~SIGEV_THREAD_ID ) !=SIGEV_NONE ) { +if (posix_timer_event (ptr,0 ) !=0 ) + ptr->it_overrun++; + }/* Re-add periodic timers */ if (ptr->it.alarm.interval.tv64) {
Signature · 1 line
1.9.1
PATCH 4/4 alarmtimer: Lock k_itimer during timer callback
In reply to John StultzFrom: Richard Larocque <[email protected]>
Locks the k_itimer's it_lock member when handling the alarm timer's expiry callback.
The regular posix timers defined in posix-timers.c have this lock held during timout processing because their callbacks are routed through posix_timer_fn(). The alarm timers follow a different path, so they ought to grab the lock somewhere else.
--- kernel/time/alarmtimer.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/time/alarmtimer.c +8 -2
@@ -464,8 +464,12 @@ static enum alarmtimer_type clock2alarm(clockid_t clockid)static enum alarmtimer_restart alarm_handle_timer (struct alarm *alarm,ktime_t now) { +unsigned long flags; struct k_itimer *ptr = container_of(alarm, struct k_itimer, it.alarm.alarmtimer); +enum alarmtimer_restart result =ALARMTIMER_NORESTART ; + +spin_lock_irqsave (&ptr->it_lock, flags);if ((ptr->it_sigev_notify & ~SIGEV_THREAD_ID ) !=SIGEV_NONE ) {if (posix_timer_event (ptr,0 ) !=0 ) ptr->it_overrun++;
@@ -475,9 +479,11 @@ static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,if (ptr->it.alarm.interval.tv64) { ptr->it_overrun +=alarm_forward (alarm, now, ptr->it.alarm.interval); -return ALARMTIMER_RESTART ; + result =ALARMTIMER_RESTART ; } -return ALARMTIMER_NORESTART ; +spin_unlock_irqrestore (&ptr->it_lock, flags); + +return result; }/**
Signature · 1 line
1.9.1