posix-cpu-timers: Bunch of fixes v2
Subsystems (2)
Frederic Weisbecker
maintainercover posix-cpu-timers: Bunch of fixes v2
This series addresses reviews from Peter. I also updated with appopriate tags:
_ Add Fixes and Stable tags on the 1st patch
_ Add Acks from Peter on two patches
_ Elaborate comment about base expiration reset on
"posix-cpu-timers: Force next_expiration recalc after timer deletion"
(per Peter)
_ Assert sighand is locked on thread_group_start_cputime()
(per Peter)
_ Factorize fix for all cases where timer doesn't get queued on
posix_cpu_timer_set() (per Peter)
_ More cleanups
git://git.kernel.org/pub/scm/linux/kernel/git/frederic/linux-dynticks.git
timers/urgent-v2
HEAD: 9cc6f1cc0e96053d709dd05b64ba5796a5b13664
Thanks,
Frederic
---
Frederic Weisbecker (7):
posix-cpu-timers: Fix rearm racing against process tick
posix-cpu-timers: Assert task sighand is locked while starting cputime counter
posix-cpu-timers: Force next_expiration recalc after timer deletion
posix-cpu-timers: Force next expiration recalc after itimer reset
posix-cpu-timers: Remove confusing error code override
posix-cpu-timers: Consolidate timer base accessor
posix-cpu-timers: Recalc next expiration when timer_settime() ends up not queueing
include/linux/posix-timers.h | 11 ++++- include/linux/sched/signal.h | 6 +++ kernel/signal.c | 13 ++++++ kernel/time/posix-cpu-timers.c | 100 ++++++++++++++++++++++++++++++++--------- 4 files changed, 106 insertions(+), 24 deletions(-)
Frederic Weisbecker
maintainerPATCH 1/7 posix-cpu-timers: Fix rearm racing against process tick
Since the process wide cputime counter is started locklessly from posix_cpu_timer_rearm(), it can be concurrently stopped by operations on other timers from the same thread group, such as in the following unlucky scenario:
CPU 0 CPU 1
----- -----
timer_settime(TIMER B)
posix_cpu_timer_rearm(TIMER A)
cpu_clock_sample_group()
(pct->timers_active already true)
handle_posix_cpu_timers()
check_process_timers()
stop_process_timers()
pct->timers_active = false
arm_timer(TIMER A) tick -> run_posix_cpu_timers()
// sees !pct->timers_active, ignore
// our TIMER AFix this with simply locking process wide cputime counting start and timer arm in the same block.
--- kernel/time/posix-cpu-timers.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/time/posix-cpu-timers.c +5 -5
@@ -991,6 +991,11 @@ static void posix_cpu_timer_rearm(struct k_itimer *timer)if (!p) goto out; +/* Protect timer list r/w in arm_timer() */ + sighand =lock_task_sighand (p, &flags); +if (unlikely (sighand ==NULL )) + goto out; +/* * Fetch the current sample and update the timer's expiry time. */
@@ -1001,11 +1006,6 @@ static void posix_cpu_timer_rearm(struct k_itimer *timer)bump_cpu_timer (timer, now); -/* Protect timer list r/w in arm_timer() */ - sighand =lock_task_sighand (p, &flags); -if (unlikely (sighand ==NULL )) - goto out; -/* * Now re-arm for the new expiry time. */
Signature · 1 line
2.25.1
Frederic Weisbecker
maintainerPATCH 2/7 posix-cpu-timers: Assert task sighand is locked while starting cputime counter
In reply to Frederic WeisbeckerStarting the process wide cputime counter needs to be done in the same sighand locking sequence than actually arming the related timer otherwise we risk races against concurrent timers setting/expiring in the same threadgroup.
Detecting that we start the cputime counter without holding the sighand lock is a first step toward debugging such situations.
--- include/linux/sched/signal.h | 6 ++++++ kernel/signal.c | 13 +++++++++++++ kernel/time/posix-cpu-timers.c | 2 ++ 3 files changed, 21 insertions(+)
include/linux/sched/signal.h +6 -0
@@ -709,6 +709,12 @@ static inline void unlock_task_sighand(struct task_struct *task,spin_unlock_irqrestore (&task->sighand->siglock, *flags); } +#ifdef CONFIG_LOCKDEP +extern void lockdep_assert_task_sighand_held (struct task_struct *task); +#else +static inline void lockdep_assert_task_sighand_held (struct task_struct *task) { } +#endif +static inline unsigned long task_rlimit (const struct task_struct *task,unsigned int limit) {
kernel/signal.c +13 -0
@@ -1440,6 +1440,19 @@ struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,return sighand; } +void lockdep_assert_task_sighand_held (struct task_struct *task) +{ +struct sighand_struct *sighand; + +rcu_read_lock (); + sighand =rcu_dereference (task->sighand); +if (sighand) +lockdep_assert_held (&sighand->siglock); +else +WARN_ON_ONCE (1 ); +rcu_read_unlock (); +} +/* * send signal info to all the members of a group */
kernel/time/posix-cpu-timers.c +2 -0
@@ -291,6 +291,8 @@ static void thread_group_start_cputime(struct task_struct *tsk, u64 *samples)struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;struct posix_cputimers *pct = &tsk->signal->posix_cputimers; +lockdep_assert_task_sighand_held (tsk); +/* Check if cputimer isn't running. This is accessed without locking. */ if (!READ_ONCE (pct->timers_active)) {struct task_cputime sum;
Signature · 1 line
2.25.1
Frederic Weisbecker
maintainerRe: [PATCH 2/7] posix-cpu-timers: Assert task sighand is locked while starting cputime counter
Quoted 10 lines · Frederic Weisbecker
kernel/signal.c +2
-0@@ -1440,6 +1440,19 @@ struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,return sighand; } +void lockdep_assert_task_sighand_held (struct task_struct *task) +{
kernel/signal.c struct sighand_struct *__lock_task_sighand(struct task_struct *tsk, · line 1454
Quoted 10 lines
kernel/signal.c +10
-0+struct sighand_struct *sighand; + +rcu_read_lock (); + sighand =rcu_dereference (task->sighand); +if (sighand) +lockdep_assert_held (&sighand->siglock); +else +WARN_ON_ONCE (1 ); +rcu_read_unlock (); +}
This wants #ifdef CONFIG_LOCKDEP
Please consider the updated patch:
---
From: Frederic Weisbecker <[email protected]>
Date: Sat, 19 Jun 2021 15:21:14 +0200
Subject: [PATCH] posix-cpu-timers: Assert task sighand is locked while
starting cputime counter
Starting the process wide cputime counter needs to be done in the same sighand locking sequence than actually arming the related timer otherwise we risk races against concurrent timers setting/expiring in the same threadgroup.
Detecting that we start the cputime counter without holding the sighand lock is a first step toward debugging such situations.
--- include/linux/sched/signal.h | 6 ++++++ kernel/signal.c | 15 +++++++++++++++ kernel/time/posix-cpu-timers.c | 2 ++ 3 files changed, 23 insertions(+)
include/linux/sched/signal.h +6 -0
@@ -709,6 +709,12 @@ static inline void unlock_task_sighand(struct task_struct *task,spin_unlock_irqrestore (&task->sighand->siglock, *flags); } +#ifdef CONFIG_LOCKDEP +extern void lockdep_assert_task_sighand_held (struct task_struct *task); +#else +static inline void lockdep_assert_task_sighand_held (struct task_struct *task) { } +#endif +static inline unsigned long task_rlimit (const struct task_struct *task,unsigned int limit) {
kernel/signal.c +15 -0
@@ -1440,6 +1440,21 @@ struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,return sighand; } +#ifdef CONFIG_LOCKDEP +void lockdep_assert_task_sighand_held (struct task_struct *task) +{ +struct sighand_struct *sighand; + +rcu_read_lock (); + sighand =rcu_dereference (task->sighand); +if (sighand) +lockdep_assert_held (&sighand->siglock); +else +WARN_ON_ONCE (1 ); +rcu_read_unlock (); +} +#endif +/* * send signal info to all the members of a group */
kernel/time/posix-cpu-timers.c +2 -0
@@ -291,6 +291,8 @@ static void thread_group_start_cputime(struct task_struct *tsk, u64 *samples)struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;struct posix_cputimers *pct = &tsk->signal->posix_cputimers; +lockdep_assert_task_sighand_held (tsk); +/* Check if cputimer isn't running. This is accessed without locking. */ if (!READ_ONCE (pct->timers_active)) {struct task_cputime sum;
Signature · 1 line
2.25.1
Frederic Weisbecker
maintainerPATCH 3/7 posix-cpu-timers: Force next_expiration recalc after timer deletion
In reply to Frederic WeisbeckerA timer deletion only dequeues the timer but it doesn't shutdown the related costly process wide cputimer counter and the tick dependency.
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);
}Make sure the next target's tick recalculates the nearest expiration and clears the process wide counter and tick dependency if necessary.
--- include/linux/posix-timers.h | 4 +++- kernel/time/posix-cpu-timers.c | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-)
include/linux/posix-timers.h +3 -1
@@ -82,12 +82,14 @@ static inline bool cpu_timer_enqueue(struct timerqueue_head *head,return timerqueue_add (head, &ctmr->node); } -static inline void cpu_timer_dequeue (struct cpu_timer *ctmr) +static inline bool cpu_timer_dequeue (struct cpu_timer *ctmr) {if (ctmr->head) {timerqueue_del (ctmr->head, &ctmr->node); ctmr->head =NULL ; +return true; } +return false; }static inline u64 cpu_timer_getexpires (struct cpu_timer *ctmr)
kernel/time/posix-cpu-timers.c +32 -1
@@ -407,6 +407,37 @@ static int posix_cpu_timer_create(struct k_itimer *new_timer)return 0 ; } +/* +* Dequeue the timer and reset the base if it was its earliest expiration. +* It makes sure the next tick recalculates the base next expiration so we +* don't keep the costly process wide cputime counter around for a random +* amount of time, along with the tick dependency. +* +* If another timer gets queued between this and the next tick, its +* expiration will update the base next event if necessary on the next +* tick. +*/ +static void disarm_timer (struct k_itimer *timer,struct task_struct *p) +{ +struct cpu_timer *ctmr = &timer->it.cpu; +struct posix_cputimer_base *base; +int clkidx; + +if (!cpu_timer_dequeue (ctmr)) +return ; + + clkidx =CPUCLOCK_WHICH (timer->it_clock); + +if (CPUCLOCK_PERTHREAD (timer->it_clock)) + base = p->posix_cputimers.bases + clkidx; +else + base = p->signal->posix_cputimers.bases + clkidx; + +if (cpu_timer_getexpires (ctmr) == base->nextevt) + base->nextevt =0 ; +} + +/* * Clean up a CPU-clock timer that is about to be destroyed. * This is called from timer deletion with the timer already locked.
@@ -441,7 +472,7 @@ static int posix_cpu_timer_del(struct k_itimer *timer)if (timer->it.cpu.firing) ret =TIMER_RETRY ;else -cpu_timer_dequeue (ctmr); +disarm_timer (timer, p);unlock_task_sighand (p, &flags); }
Signature · 1 line
2.25.1
Frederic Weisbecker
maintainerPATCH 4/7 posix-cpu-timers: Force next expiration recalc after itimer reset
In reply to Frederic WeisbeckerWhen an itimer deactivates a previously armed expiration, it simply doesn't do anything. As a result the process wide cputime counter keeps running and the tick dependency stays set until we reach the old ghost expiration value.
This can be reproduced with the following snippet:
void trigger_process_counter(void)
{
struct itimerval n = {};
n.it_value.tv_sec = 100;
setitimer(ITIMER_VIRTUAL, &n, NULL);
n.it_value.tv_sec = 0;
setitimer(ITIMER_VIRTUAL, &n, NULL);
}Fix this with resetting the relevant base expiration. This is similar to disarming a timer.
--- kernel/time/posix-cpu-timers.c | 2 -- 1 file changed, 2 deletions(-)
kernel/time/posix-cpu-timers.c +0 -2
@@ -1379,8 +1379,6 @@ void set_process_cpu_timer(struct task_struct *tsk, unsigned int clkid, } } -if (!*newval) -return ; *newval += now; }
Signature · 1 line
2.25.1
Frederic Weisbecker
maintainerPATCH 5/7 posix-cpu-timers: Remove confusing error code override
In reply to Frederic WeisbeckerWe can't reach this end of the function with an error. Unconfuse reviewers about that.
--- kernel/time/posix-cpu-timers.c | 2 -- 1 file changed, 2 deletions(-)
kernel/time/posix-cpu-timers.c +0 -2
@@ -744,8 +744,6 @@ static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags,*/ cpu_timer_fire (timer); } - - ret =0 ; out:rcu_read_unlock ();if (old)
Signature · 1 line
2.25.1
Frederic Weisbecker
maintainerPATCH 6/7 posix-cpu-timers: Consolidate timer base accessor
In reply to Frederic WeisbeckerRemove the ad-hoc timer base accessors and provide a consolidated one.
--- kernel/time/posix-cpu-timers.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-)
kernel/time/posix-cpu-timers.c +13 -15
@@ -407,6 +407,17 @@ static int posix_cpu_timer_create(struct k_itimer *new_timer)return 0 ; } +static struct posix_cputimer_base *timer_base (struct k_itimer *timer, +struct task_struct *tsk) +{ +int clkidx =CPUCLOCK_WHICH (timer->it_clock); + +if (CPUCLOCK_PERTHREAD (timer->it_clock)) +return tsk->posix_cputimers.bases + clkidx; +else +return tsk->signal->posix_cputimers.bases + clkidx; +} +/* * Dequeue the timer and reset the base if it was its earliest expiration. * It makes sure the next tick recalculates the base next expiration so we
@@ -421,18 +432,11 @@ static void disarm_timer(struct k_itimer *timer, struct task_struct *p) {struct cpu_timer *ctmr = &timer->it.cpu;struct posix_cputimer_base *base; -int clkidx;if (!cpu_timer_dequeue (ctmr))return ; - clkidx =CPUCLOCK_WHICH (timer->it_clock); - -if (CPUCLOCK_PERTHREAD (timer->it_clock)) - base = p->posix_cputimers.bases + clkidx; -else - base = p->signal->posix_cputimers.bases + clkidx; - + base =timer_base (timer, p);if (cpu_timer_getexpires (ctmr) == base->nextevt) base->nextevt =0 ; }
@@ -531,15 +535,9 @@ void posix_cpu_timers_exit_group(struct task_struct *tsk)*/ static void arm_timer (struct k_itimer *timer,struct task_struct *p) { -int clkidx =CPUCLOCK_WHICH (timer->it_clock); +struct posix_cputimer_base *base =timer_base (timer, p);struct cpu_timer *ctmr = &timer->it.cpu;u64 newexp =cpu_timer_getexpires (ctmr); -struct posix_cputimer_base *base; - -if (CPUCLOCK_PERTHREAD (timer->it_clock)) - base = p->posix_cputimers.bases + clkidx; -else - base = p->signal->posix_cputimers.bases + clkidx;if (!cpu_timer_enqueue (&base->tqhead, ctmr))return ;
Signature · 1 line
2.25.1
Frederic Weisbecker
maintainerPATCH 7/7 posix-cpu-timers: Recalc next expiration when timer_settime() ends up not queueing
In reply to Frederic WeisbeckerThere are several scenarios that can result in posix_cpu_timer_set() not queueing the timer but still leave the threadroup cputime counter running or keep 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().
--- include/linux/posix-timers.h | 7 +++++- kernel/time/posix-cpu-timers.c | 41 +++++++++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 7 deletions(-)
include/linux/posix-timers.h +6 -1
@@ -82,9 +82,14 @@ static inline bool cpu_timer_enqueue(struct timerqueue_head *head,return timerqueue_add (head, &ctmr->node); } +static inline bool cpu_timer_queued (struct cpu_timer *ctmr) +{ +return !!ctmr->head; +} +static inline bool cpu_timer_dequeue (struct cpu_timer *ctmr) { -if (ctmr->head) { +if (cpu_timer_queued (ctmr)) {timerqueue_del (ctmr->head, &ctmr->node); ctmr->head =NULL ;return true;
kernel/time/posix-cpu-timers.c +35 -6
61 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH 7/7] posix-cpu-timers: Recalc next expiration when timer_settime() ends up not queueing
Quoted 9 lines · Frederic Weisbecker
include/linux/posix-timers.h | 7 +++++- kernel/time/posix-cpu-timers.c | 41 +++++++++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 7 deletions(-)include/linux/posix-timers.h +0
-0
include/linux/posix-timers.h static inline bool cpu_timer_enqueue(struct timerqueue_head *head, · line 87
Quoted 6 lines
include/linux/posix-timers.h +3
-0@@ -82,9 +82,14 @@ static inline bool cpu_timer_enqueue(struct timerqueue_head *head,return timerqueue_add (head, &ctmr->node); } +static inline bool cpu_timer_queued (struct cpu_timer *ctmr) +{ +return !!ctmr->head;
Sad that we can't check sighand lock here... but I agree that adding that will be messy :/
Quoted 1 line
include/linux/posix-timers.h +1
-0+}
Quoted 8 lines · Frederic Weisbecker
Frederic Weisbecker (7):
posix-cpu-timers: Fix rearm racing against process tick
posix-cpu-timers: Assert task sighand is locked while starting cputime counter
posix-cpu-timers: Forcenext_expirationrecalc after timer deletion
posix-cpu-timers: Force next expiration recalc after itimer reset
posix-cpu-timers: Remove confusing error code override
posix-cpu-timers: Consolidate timer base accessor
posix-cpu-timers: Recalc next expiration whentimer_settime()ends up not queueing
Looks good, Thanks!