Skip to content

Commit ee37532

Browse files
Frederic WeisbeckerKAGA-KOKO
authored andcommitted
posix-cpu-timers: Recalc next expiration when timer_settime() ends up not queueing
There are several scenarios that can result in posix_cpu_timer_set() not queueing the timer but still leaving the threadgroup cputime counter running or keeping the tick dependency around for a random amount of time. 1) If timer_settime() is called with a 0 expiration on a timer that is already disabled, the process wide cputime counter will be started and won't ever get a chance to be stopped by stop_process_timer() since no timer is actually armed to be processed. The following snippet is enough to trigger the issue. void trigger_process_counter(void) { timer_t id; struct itimerspec val = { }; timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id); timer_settime(id, TIMER_ABSTIME, &val, NULL); timer_delete(id); } 2) If timer_settime() is called with a 0 expiration on a timer that is already armed, the timer is dequeued but not really disarmed. So the process wide cputime counter and the tick dependency may still remain a while around. The following code snippet keeps this overhead around for one week after the timer deletion: void trigger_process_counter(void) { timer_t id; struct itimerspec val = { }; val.it_value.tv_sec = 604800; timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id); timer_settime(id, 0, &val, NULL); timer_delete(id); } 3) If the timer was initially deactivated, this call to timer_settime() with an early expiration may have started the process wide cputime counter even though the timer hasn't been queued and armed because it has fired early and inline within posix_cpu_timer_set() itself. As a result the process wide cputime counter may never stop until a new timer is ever armed in the future. The following code snippet can reproduce this: void trigger_process_counter(void) { timer_t id; struct itimerspec val = { }; signal(SIGALRM, SIG_IGN); timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id); val.it_value.tv_nsec = 1; timer_settime(id, TIMER_ABSTIME, &val, NULL); } 4) If the timer was initially armed with a former expiration value before this call to timer_settime() and the current call sets an early deadline that has already expired, the timer fires inline within posix_cpu_timer_set(). In this case it must have been dequeued before firing inline with its new expiration value, yet it hasn't been disarmed in this case. So the process wide cputime counter and the tick dependency may still be around for a while even after the timer fired. The following code snippet can reproduce this: void trigger_process_counter(void) { timer_t id; struct itimerspec val = { }; signal(SIGALRM, SIG_IGN); timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id); val.it_value.tv_sec = 100; timer_settime(id, TIMER_ABSTIME, &val, NULL); val.it_value.tv_sec = 0; val.it_value.tv_nsec = 1; timer_settime(id, TIMER_ABSTIME, &val, NULL); } Fix all these issues with triggering the related base next expiration recalculation on the next tick. This also implies to re-evaluate the need to keep around the process wide cputime counter and the tick dependency, in a similar fashion to disarm_timer(). Suggested-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: Frederic Weisbecker <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Acked-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 5c8f23e commit ee37532

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

include/linux/posix-timers.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,14 @@ static inline bool cpu_timer_enqueue(struct timerqueue_head *head,
8282
return timerqueue_add(head, &ctmr->node);
8383
}
8484

85+
static inline bool cpu_timer_queued(struct cpu_timer *ctmr)
86+
{
87+
return !!ctmr->head;
88+
}
89+
8590
static inline bool cpu_timer_dequeue(struct cpu_timer *ctmr)
8691
{
87-
if (ctmr->head) {
92+
if (cpu_timer_queued(ctmr)) {
8893
timerqueue_del(ctmr->head, &ctmr->node);
8994
ctmr->head = NULL;
9095
return true;

kernel/time/posix-cpu-timers.c

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,20 @@ static struct posix_cputimer_base *timer_base(struct k_itimer *timer,
418418
return tsk->signal->posix_cputimers.bases + clkidx;
419419
}
420420

421+
/*
422+
* Force recalculating the base earliest expiration on the next tick.
423+
* This will also re-evaluate the need to keep around the process wide
424+
* cputime counter and tick dependency and eventually shut these down
425+
* if necessary.
426+
*/
427+
static void trigger_base_recalc_expires(struct k_itimer *timer,
428+
struct task_struct *tsk)
429+
{
430+
struct posix_cputimer_base *base = timer_base(timer, tsk);
431+
432+
base->nextevt = 0;
433+
}
434+
421435
/*
422436
* Dequeue the timer and reset the base if it was its earliest expiration.
423437
* It makes sure the next tick recalculates the base next expiration so we
@@ -438,7 +452,7 @@ static void disarm_timer(struct k_itimer *timer, struct task_struct *p)
438452

439453
base = timer_base(timer, p);
440454
if (cpu_timer_getexpires(ctmr) == base->nextevt)
441-
base->nextevt = 0;
455+
trigger_base_recalc_expires(timer, p);
442456
}
443457

444458

@@ -734,13 +748,28 @@ static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags,
734748
timer->it_overrun_last = 0;
735749
timer->it_overrun = -1;
736750

737-
if (new_expires != 0 && !(val < new_expires)) {
751+
if (val >= new_expires) {
752+
if (new_expires != 0) {
753+
/*
754+
* The designated time already passed, so we notify
755+
* immediately, even if the thread never runs to
756+
* accumulate more time on this clock.
757+
*/
758+
cpu_timer_fire(timer);
759+
}
760+
738761
/*
739-
* The designated time already passed, so we notify
740-
* immediately, even if the thread never runs to
741-
* accumulate more time on this clock.
762+
* Make sure we don't keep around the process wide cputime
763+
* counter or the tick dependency if they are not necessary.
742764
*/
743-
cpu_timer_fire(timer);
765+
sighand = lock_task_sighand(p, &flags);
766+
if (!sighand)
767+
goto out;
768+
769+
if (!cpu_timer_queued(ctmr))
770+
trigger_base_recalc_expires(timer, p);
771+
772+
unlock_task_sighand(p, &flags);
744773
}
745774
out:
746775
rcu_read_unlock();

0 commit comments

Comments
 (0)