posix cpu timers updates
Subsystems (4)
Thomas,
Please pull the timers/posix-cpu-timers-for-tglx branch that can be found at:
git://git.kernel.org/pub/scm/linux/kernel/git/frederic/linux-dynticks.git
timers/posix-cpu-timers-for-tglx
Most of these patches have been hanging around for several month now, in -mmotm for a significant chunk. They already missed a few releases.
This pile includes quite a part of these. Kosaki also has some pending patches that we are still discussing a bit. But I'll try to take care of these as well.
Thanks,
Frederic
---
Frederic Weisbecker (6):
posix_cpu_timer: consolidate expiry time type
posix_cpu_timers: consolidate timer list cleanups
posix_cpu_timers: consolidate expired timers check
selftests: add basic posix timers selftests
posix-timers: correctly get dying task time sample in posix_cpu_timer_schedule()
posix_timers: fix racy timer delta caching on task exit
KOSAKI Motohiro (1):
posix-cpu-timers: don't account cpu timer after stopped thread runtime accounting
include/linux/posix-timers.h | 16 +- kernel/posix-cpu-timers.c | 395 +++++++++---------------- kernel/sched/stats.h | 39 +++- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/timers/Makefile | 8 + tools/testing/selftests/timers/posix_timers.c | 221 ++++++++++++++ 6 files changed, 417 insertions(+), 263 deletions(-)
PATCH 1/7 posix_cpu_timer: consolidate expiry time type
The posix cpu timer expiry time is stored in a union of two types: a 64 bits field if we rely on scheduler precise accounting, or a cputime_t if we rely on jiffies.
This results in quite some duplicate code and special cases to handle the two types.
Just unify this into a single 64 bits field. cputime_t can always fit into it.
--- include/linux/posix-timers.h | 16 ++- kernel/posix-cpu-timers.c | 266 +++++++++++++++++------------------------- 2 files changed, 117 insertions(+), 165 deletions(-)
include/linux/posix-timers.h +11 -5
@@ -7,14 +7,20 @@#include <linux/timex.h> #include <linux/alarmtimer.h> -union cpu_time_count { -cputime_t cpu; -unsigned long long sched; -}; + +static inline unsigned long long cputime_to_expires (cputime_t expires) +{ +return (__force unsigned long long )expires; +} + +static inline cputime_t expires_to_cputime (unsigned long long expires) +{ +return (__force cputime_t )expires; +}struct cpu_timer_list {struct list_head entry; -union cpu_time_count expires, incr; +unsigned long long expires, incr;struct task_struct *task;int firing; };
kernel/posix-cpu-timers.c +106 -160
601 lines not on this page · raw message
Signature · 1 line
1.7.5.4
PATCH 2/7 posix_cpu_timers: consolidate timer list cleanups
In reply to Frederic WeisbeckerCleaning up the posix cpu timers on task exit shares some common code among timer list types, most notably the list traversal and expiry time update.
Unify this in a common helper.
--- kernel/posix-cpu-timers.c | 48 +++++++++++++++++--------------------------- 1 files changed, 19 insertions(+), 29 deletions(-)
kernel/posix-cpu-timers.c +19 -29
@@ -399,6 +399,21 @@ static int posix_cpu_timer_del(struct k_itimer *timer)return ret; } +static void cleanup_timers_list (struct list_head *head, +unsigned long long curr) +{ +struct cpu_timer_list *timer, *next; + +list_for_each_entry_safe (timer, next, head, entry) { +list_del_init (&timer->entry); +if (timer->expires < curr) { + timer->expires =0 ; + }else { + timer->expires -= curr; + } + } +} +/* * Clean out CPU timers still ticking when a thread exited. The task * pointer is cleared, and the expiry time is replaced with the residual
@@ -409,37 +424,12 @@ static void cleanup_timers(struct list_head *head, cputime_t utime, cputime_t stime, unsigned long long sum_exec_runtime) { - struct cpu_timer_list *timer, *next; -cputime_t ptime = utime + stime; -list_for_each_entry_safe (timer, next, head, entry) { -list_del_init (&timer->entry); -if (timer->expires <cputime_to_expires (ptime)) { - timer->expires =0 ; - }else { - timer->expires -=cputime_to_expires (ptime); - } - } - - ++head; -list_for_each_entry_safe (timer, next, head, entry) { -list_del_init (&timer->entry); -if (timer->expires <cputime_to_expires (utime)) { - timer->expires =0 ; - }else { - timer->expires -=cputime_to_expires (utime); - } - } + cputime_t ptime = utime + stime; - ++head; -list_for_each_entry_safe (timer, next, head, entry) { -list_del_init (&timer->entry); -if (timer->expires < sum_exec_runtime) { - timer->expires =0 ; - }else { - timer->expires -= sum_exec_runtime; - } - } +cleanup_timers_list (head,cputime_to_expires (ptime)); +cleanup_timers_list (++head,cputime_to_expires (utime)); +cleanup_timers_list (++head, sum_exec_runtime); }/*
Signature · 1 line
1.7.5.4
PATCH 3/7 posix_cpu_timers: consolidate expired timers check
In reply to Frederic WeisbeckerConsolidate the common code amongst per thread and per process timers list on tick time.
List traversal, expiry check and subsequent updates can be shared in a common helper.
--- kernel/posix-cpu-timers.c | 118 +++++++++++++-------------------------------- 1 files changed, 33 insertions(+), 85 deletions(-)
kernel/posix-cpu-timers.c +33 -85
@@ -862,6 +862,28 @@ static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp) } } +static unsigned long long +check_timers_list (struct list_head *timers, +struct list_head *firing, +unsigned long long curr) +{ +int maxfire =20 ; + +while (!list_empty (timers)) { +struct cpu_timer_list *t; + + t = list_first_entry(timers, struct cpu_timer_list, entry); + +if (!--maxfire || curr < t->expires) +return t->expires; + + t->firing =1 ; +list_move_tail (&t->entry, firing); + } + +return 0 ; +} +/* * Check for any per-thread CPU timers that have fired and move them off * the tsk->cpu_timers[N] list onto the firing list. Here we update the
@@ -870,54 +892,20 @@ static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp)static void check_thread_timers (struct task_struct *tsk,struct list_head *firing) { -int maxfire;struct list_head *timers = tsk->cpu_timers;struct signal_struct *const sig = tsk->signal; +struct task_cputime *tsk_expires = &tsk->cputime_expires; +unsigned long long expires;unsigned long soft; - maxfire =20 ; - tsk->cputime_expires.prof_exp =0 ; -while (!list_empty (timers)) { -struct cpu_timer_list *t =list_first_entry (timers, - struct cpu_timer_list, - entry); -if (!--maxfire ||prof_ticks (tsk) < t->expires) { - tsk->cputime_expires.prof_exp =expires_to_cputime (t->expires); -break ; - } - t->firing =1 ; -list_move_tail (&t->entry, firing); - } + expires =check_timers_list (timers, firing,prof_ticks (tsk)); + tsk_expires->prof_exp =expires_to_cputime (expires); - ++timers; - maxfire =20 ; - tsk->cputime_expires.virt_exp =0 ; -while (!list_empty (timers)) { -struct cpu_timer_list *t =list_first_entry (timers, - struct cpu_timer_list, - entry); -if (!--maxfire ||virt_ticks (tsk) < t->expires) { - tsk->cputime_expires.virt_exp =expires_to_cputime (t->expires); -break ; - } - t->firing =1 ; -list_move_tail (&t->entry, firing); - } + expires =check_timers_list (++timers, firing,virt_ticks (tsk)); + tsk_expires->virt_exp =expires_to_cputime (expires); - ++timers; - maxfire =20 ; - tsk->cputime_expires.sched_exp =0 ; -while (!list_empty (timers)) { -struct cpu_timer_list *t =list_first_entry (timers, - struct cpu_timer_list, - entry); -if (!--maxfire || tsk->se.sum_exec_runtime < t->expires) { - tsk->cputime_expires.sched_exp = t->expires; -break ; - } - t->firing =1 ; -list_move_tail (&t->entry, firing); - } + tsk_expires->sched_exp =check_timers_list (++timers, firing, + tsk->se.sum_exec_runtime);/* * Check for the special case thread timers.
@@ -1002,7 +990,6 @@ static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it,static void check_process_timers (struct task_struct *tsk,struct list_head *firing) { -int maxfire;struct signal_struct *const sig = tsk->signal;unsigned long long utime, ptime, virt_expires, prof_expires;unsigned long long sum_sched_runtime, sched_expires;
@@ -1017,49 +1004,10 @@ static void check_process_timers(struct task_struct *tsk, utime =cputime_to_expires (cputime.utime); ptime = utime +cputime_to_expires (cputime.stime); sum_sched_runtime = cputime.sum_exec_runtime; - maxfire =20 ; - prof_expires =0 ; -while (!list_empty (timers)) { -struct cpu_timer_list *tl =list_first_entry (timers, - struct cpu_timer_list, - entry); -if (!--maxfire || ptime < tl->expires) { - prof_expires = tl->expires; -break ; - } - tl->firing =1 ; -list_move_tail (&tl->entry, firing); - } - ++timers; - maxfire =20 ; - virt_expires =0 ; -while (!list_empty (timers)) { -struct cpu_timer_list *tl =list_first_entry (timers, - struct cpu_timer_list, - entry); -if (!--maxfire || utime < tl->expires) { - virt_expires = tl->expires; -break ; - } - tl->firing =1 ; -list_move_tail (&tl->entry, firing); - } - - ++timers; - maxfire =20 ; - sched_expires =0 ; -while (!list_empty (timers)) { -struct cpu_timer_list *tl =list_first_entry (timers, - struct cpu_timer_list, - entry); -if (!--maxfire || sum_sched_runtime < tl->expires) { - sched_expires = tl->expires; -break ; - } - tl->firing =1 ; -list_move_tail (&tl->entry, firing); - } + prof_expires =check_timers_list (timers, firing, ptime); + virt_expires =check_timers_list (++timers, firing, utime); + sched_expires =check_timers_list (++timers, firing, sum_sched_runtime);/* * Check for the special case process timers.
Signature · 1 line
1.7.5.4
PATCH 4/7 selftests: add basic posix timers selftests
In reply to Frederic WeisbeckerAdd some initial basic tests on a few posix timers interface such as setitimer() and timer_settime().
These simply check that expiration happens in a reasonable timeframe after expected elapsed clock time (user time, user + system time, real time, ...).
This is helpful for finding basic breakages while hacking on this subsystem.
--- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/timers/Makefile | 8 + tools/testing/selftests/timers/posix_timers.c | 221 +++++++++++++++++++++++++ 3 files changed, 230 insertions(+), 0 deletions(-) create mode 100644 tools/testing/selftests/timers/Makefile create mode 100644 tools/testing/selftests/timers/posix_timers.c
tools/testing/selftests/Makefile +1 -0
@@ -6,6 +6,7 @@ TARGETS += memory-hotplug TARGETS += mqueue TARGETS += net TARGETS += ptrace +TARGETS += timers TARGETS += vm all:
tools/testing/selftests/timers/Makefile new file +8 -0
@@ -0,0 +1,8 @@ +all: + gcc posix_timers.c -o posix_timers -lrt + +run_tests: all + ./posix_timers + +clean: + rm -f ./posix_timers
tools/testing/selftests/timers/posix_timers.c new file +221 -0
221 lines not on this page · raw message
Signature · 1 line
1.7.5.4
PATCH 5/7 posix-timers: correctly get dying task time sample in posix_cpu_timer_schedule()
In reply to Frederic WeisbeckerIn order to re-arm a timer after it fired, we take a sample of the current process or thread cputime.
If the task is dying though, we don't arm anything but we cache the remaining timer expiration delta for further reads.
Something similar is performed in posix_cpu_timer_get() but here we forget to take the process wide cputime sample before caching it.
As a result we are storing random stack content, leading every further reads of that timer to return junk values.
Fix this by taking the appropriate sample in the case of process wide timers.
This probably doesn't matter much in practice because, at this stage, the thread is the last one in the group and we reached exit_notify(). This implies that we called exit_itimers() and there should be no more timers to handle for that task.
So this is likely dead code anyway but let's fix the current logic and the warning that came along:
kernel/posix-cpu-timers.c: In function 'posix_cpu_timer_schedule':
kernel/posix-cpu-timers.c:1127: warning: 'now' may be used uninitialized in this function
Then we can start to think further about cleaning up that code.
--- kernel/posix-cpu-timers.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
kernel/posix-cpu-timers.c +1 -0
@@ -1097,6 +1097,7 @@ void posix_cpu_timer_schedule(struct k_itimer *timer)* not yet reaped. Take this opportunity to * drop our task ref. */ +cpu_timer_sample_group (timer->it_clock, p, &now);clear_dead_task (timer, now); goto out_unlock; }
Signature · 1 line
1.7.5.4
PATCH 6/7 posix_timers: fix racy timer delta caching on task exit
In reply to Frederic WeisbeckerWhen a task exits, we perform a caching of the remaining cputime delta before expiring of its timers.
This is done from the following places:
* When the task is reaped. We iterate through its list of posix cpu timers and store the remaining timer delta to the timer struct instead of the absolute value.
(See posix_cpu_timers_exit() / posix_cpu_timers_exit_group() )
* When we call posix_cpu_timer_get() or posix_cpu_timer_schedule(). If the timer's task is considered dying when watched from these places, the same conversion from absolute to relative expiry time is performed. Then the given task's reference is released. (See clear_dead_task() ).
The relevance of this caching is questionable but this is another and deeper debate.
The big issue here is that these two sources of caching don't mix up very well together.
More specifically, the caching can easily be done twice, resulting in a wrong delta as it gets spuriously substracted a second time by the elapsed clock. This can happen in the following scenario:
1) The task exits and gets reaped: we call posix_cpu_timers_exit() and the absolute timer expiry values are converted to a relative delta.
2) timer_gettime() -> posix_cpu_timer_get() is called and relies on clear_dead_task() because tsk->exit_state == EXIT_DEAD. The delta gets substracted again by the elapsed clock and we return a wrong result.
To fix this, just remove the caching done on task reaping time. It doesn't bring much value on its own. The caching done from posix_cpu_timer_get/schedule is enough.
And it would also be hard to get it really right: we could make it put and clear the target task in the timer struct so that readers know if they are dealing with a relative cached of absolute value. But it would be racy. The only safe way to do it would be to lock the itimer->it_lock so that we know nobody reads the cputime expiry value while we modify it and its target task reference. Doing so would involve some funny workarounds to avoid circular lock against the sighand lock. There is just no reason to maintain this.
The user visible effect of this patch can be observed by running the following code: it creates a subthread that launches a posix cputimer which expires after 10 seconds. But then the subthread only busy loops for 2 seconds and exits. The parent reaps the subthread and read the timer value. Its expected value should the be the initial timer's expiration value minus the cputime elapsed in the subthread. Roughly 10 - 2 = 8 seconds:
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
static timer_t id;
static struct itimerspec val = { .it_value.tv_sec = 10, }, new;
static void *thread(void *unused)
{
int err;
struct timeval start, end, diff;
timer_create(CLOCK_THREAD_CPUTIME_ID, NULL, &id);
if (err < 0) {
perror("Can't create timer\n");
return NULL;
}
/* Arm 10 sec timer */
err = timer_settime(id, 0, &val, NULL);
if (err < 0) {
perror("Can't set timer\n");
return NULL;
}
/* Exit after 2 seconds of execution */
gettimeofday(&start, NULL);
do {
gettimeofday(&end, NULL);
timersub(&end, &start, &diff);
} while (diff.tv_sec < 2);
return NULL;
}
int main(int argc, char **argv)
{
pthread_t pthread;
int err;
err = pthread_create(&pthread, NULL, thread, NULL);
if (err) {
perror("Can't create thread\n");
return -1;
}
pthread_join(pthread, NULL);
/* Just wait a little bit to make sure the child got reaped */
sleep(1);
err = timer_gettime(id, &new);
if (err)
perror("Can't get timer value\n");
printf("%d %ld\n", new.it_value.tv_sec, new.it_value.tv_nsec);
return 0;
}Before the patch:
$ ./posix_cpu_timers
6 2278074After the patch:
$ ./posix_cpu_timers
8 1158766Before the patch, the elapsed time got two more seconds spuriously accounted.
--- kernel/posix-cpu-timers.c | 22 +++++++++++----------- 1 files changed, 11 insertions(+), 11 deletions(-)
kernel/posix-cpu-timers.c +11 -11
@@ -404,14 +404,8 @@ static void cleanup_timers_list(struct list_head *head, {struct cpu_timer_list *timer, *next; -list_for_each_entry_safe (timer, next, head, entry) { +list_for_each_entry_safe (timer, next, head, entry)list_del_init (&timer->entry); -if (timer->expires < curr) { - timer->expires =0 ; - }else { - timer->expires -= curr; - } - } }/*
@@ -459,15 +453,21 @@ void posix_cpu_timers_exit_group(struct task_struct *tsk) tsk->se.sum_exec_runtime + sig->sum_sched_runtime); } -static void clear_dead_task (struct k_itimer *timer,unsigned long long now) +static void clear_dead_task (struct k_itimer *itimer,unsigned long long now) { +struct cpu_timer_list *timer = &itimer->it.cpu; +/* * That's all for this thread or process. * We leave our residual in expires to be reported. */ -put_task_struct (timer->it.cpu.task); - timer->it.cpu.task =NULL ; - timer->it.cpu.expires -= now; +put_task_struct (timer->task); + timer->task =NULL ; +if (timer->expires < now) { + timer->expires =0 ; + }else { + timer->expires -= now; + } }static inline int expires_gt (cputime_t expires,cputime_t new_exp)
Signature · 1 line
1.7.5.4
PATCH 7/7 posix-cpu-timers: don't account cpu timer after stopped thread runtime accounting
In reply to Frederic WeisbeckerFrom: KOSAKI Motohiro <[email protected]>
When tsk->signal->cputimer->running is 1, signal->cputimer (i.e. per process timer account) and tsk->sum_sched_runtime (i.e. per thread timer account) increase at the same pace because update_curr() increases both accounting.
However, there is one exception. When thread exiting, __exit_signal() turns over task's sum_shced_runtime to sig->sum_sched_runtime, but it doesn't stop signal->cputimer accounting.
This inconsistency makes POSIX timer wake up too early. This patch fixes it.
--- kernel/sched/stats.h | 39 ++++++++++++++++++++++++++++++++++++--- 1 files changed, 36 insertions(+), 3 deletions(-)
kernel/sched/stats.h +36 -3
@@ -162,6 +162,39 @@ sched_info_switch(struct task_struct *prev, struct task_struct *next)*/ /** +* cputimer_running - return true if cputimer is running +* +* @tsk: Pointer to target task. +*/ +static inline bool cputimer_running (struct task_struct *tsk) + +{ +struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; + +if (!cputimer->running) +return false; + +/* +* After we flush the task's sum_exec_runtime to sig->sum_sched_runtime +* in __exit_signal(), we won't account to the signal struct further +* cputime consumed by that task, even though the task can still be +* ticking after __exit_signal(). +* +* In order to keep a consistent behaviour between thread group cputime +* and thread group cputimer accounting, lets also ignore the cputime +* elapsing after __exit_signal() in any thread group timer running. +* +* This makes sure that POSIX CPU clocks and timers are synchronized, so +* that a POSIX CPU timer won't expire while the corresponding POSIX CPU +* clock delta is behind the expiring timer value. +*/ +if (unlikely (!tsk->sighand)) +return false; + +return true; +} + +/** * account_group_user_time - Maintain utime for a thread group. * * @tsk: Pointer to task structure.
@@ -176,7 +209,7 @@ static inline void account_group_user_time(struct task_struct *tsk, {struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; -if (!cputimer->running) +if (!cputimer_running (tsk))return ;raw_spin_lock (&cputimer->lock);
@@ -199,7 +232,7 @@ static inline void account_group_system_time(struct task_struct *tsk, {struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; -if (!cputimer->running) +if (!cputimer_running (tsk))return ;raw_spin_lock (&cputimer->lock);
@@ -222,7 +255,7 @@ static inline void account_group_exec_runtime(struct task_struct *tsk, {struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; -if (!cputimer->running) +if (!cputimer_running (tsk))return ;raw_spin_lock (&cputimer->lock);
Signature · 1 line
1.7.5.4