posix-cpu-timers: fix counting delta_exec twice
Subsystems (3)
PATCH 1/1 posix-cpu-timers: fix counting delta_exec twice
From: KOSAKI Motohiro <[email protected]>
Currently glibc rt/cpuclock2 test(*) sporadically fail. The pseudo test code is here.
t0 = clock_gettime()
abs = t0 + sleeptime;
clock_nanosleep(TIMER_ABSTIME, abs)
t1 = clock_gettime()
assert(t1-t0 > sleeptime)
assert(t1 > abs)
Because current signal->cputimer->cputime logic can add delta_exec twice wrongly. The first is in initialization, thread_group_cputimer() call thread_group_cputime() and it adds delta_exec. and later, update_curr() adds delta_exec again.
Finally, clock_nanosleep() wakes up slightly before than passed argument and it is posix violation.
This issue was introduced by commit d670ec1317 (posix-cpu-timers:
Cure SMP wobbles).
Cc Peter Zijlstra <[email protected]>
Cc David Miller <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Signed-off-by KOSAKI Motohiro <[email protected]>
--- fs/binfmt_elf.c | 2 +- fs/binfmt_elf_fdpic.c | 2 +- include/linux/sched.h | 4 ++-- kernel/posix-cpu-timers.c | 10 +++++----- kernel/sched/core.c | 6 ++++-- kernel/sched/cputime.c | 8 ++++---- 6 files changed, 17 insertions(+), 15 deletions(-)
fs/binfmt_elf.c +1 -1
@@ -1321,7 +1321,7 @@ static void fill_prstatus(struct elf_prstatus *prstatus,* This is the record for the group leader. It shows the * group-wide total, not its individual thread total. */ -thread_group_cputime (p, &cputime); +thread_group_cputime (p, true, &cputime);cputime_to_timeval (cputime.utime, &prstatus->pr_utime);cputime_to_timeval (cputime.stime, &prstatus->pr_stime); }else {
fs/binfmt_elf_fdpic.c +1 -1
@@ -1371,7 +1371,7 @@ static void fill_prstatus(struct elf_prstatus *prstatus,* This is the record for the group leader. It shows the * group-wide total, not its individual thread total. */ -thread_group_cputime (p, &cputime); +thread_group_cputime (p, true, &cputime);cputime_to_timeval (cputime.utime, &prstatus->pr_utime);cputime_to_timeval (cputime.stime, &prstatus->pr_stime); }else {
include/linux/sched.h +2 -2
@@ -2001,7 +2001,7 @@ static inline void disable_sched_clock_irqtime(void) {}#endif extern unsigned long long -task_sched_runtime (struct task_struct *task); +task_sched_runtime (struct task_struct *task,bool use_delta);/* sched_exec is called by processes performing an exec */ #ifdef CONFIG_SMP
@@ -2624,7 +2624,7 @@ static inline int spin_needbreak(spinlock_t *lock)/* * Thread group CPU time accounting. */ -void thread_group_cputime (struct task_struct *tsk,struct task_cputime *times); +void thread_group_cputime (struct task_struct *tsk,bool use_delta,struct task_cputime *times);void thread_group_cputimer (struct task_struct *tsk,struct task_cputime *times);static inline void thread_group_cputime_init (struct signal_struct *sig)
kernel/posix-cpu-timers.c +5 -5
@@ -220,7 +220,7 @@ static int cpu_clock_sample(const clockid_t which_clock, struct task_struct *p, cpu->cpu =virt_ticks (p);break ;case CPUCLOCK_SCHED : - cpu->sched =task_sched_runtime (p); + cpu->sched =task_sched_runtime (p, true);break ; }return 0 ;
@@ -251,7 +251,7 @@ void thread_group_cputimer(struct task_struct *tsk, struct task_cputime *times)* to synchronize the timer to the clock every time we start * it. */ -thread_group_cputime (tsk, &sum); +thread_group_cputime (tsk, false, &sum);raw_spin_lock_irqsave (&cputimer->lock, flags); cputimer->running =1 ;update_gt_cputime (&cputimer->cputime, &sum);
@@ -275,15 +275,15 @@ static int cpu_clock_sample_group(const clockid_t which_clock,default :return -EINVAL ;case CPUCLOCK_PROF : -thread_group_cputime (p, &cputime); +thread_group_cputime (p, true, &cputime); cpu->cpu = cputime.utime + cputime.stime;break ;case CPUCLOCK_VIRT : -thread_group_cputime (p, &cputime); +thread_group_cputime (p, true, &cputime); cpu->cpu = cputime.utime;break ;case CPUCLOCK_SCHED : -thread_group_cputime (p, &cputime); +thread_group_cputime (p, true, &cputime); cpu->sched = cputime.sum_exec_runtime;break ; }
kernel/sched/core.c +4 -2
@@ -2662,14 +2662,16 @@ unsigned long long task_delta_exec(struct task_struct *p)* In case the task is currently running, return the runtime plus current's * pending runtime that have not been accounted yet. */ -unsigned long long task_sched_runtime (struct task_struct *p) +unsigned long long task_sched_runtime (struct task_struct *p,bool use_delta) {unsigned long flags;struct rq *rq;u64 ns =0 ; rq =task_rq_lock (p, &flags); - ns = p->se.sum_exec_runtime +do_task_delta_exec (p, rq); + ns = p->se.sum_exec_runtime; +if (use_delta) + ns +=do_task_delta_exec (p, rq);task_rq_unlock (rq, p, &flags);return ns;
kernel/sched/cputime.c +4 -4
@@ -293,7 +293,7 @@ static __always_inline bool steal_account_process_tick(void)* Accumulate raw cputime values of dead tasks (sig->[us]time) and live * tasks (sum on group iteration) belonging to @tsk's group. */ -void thread_group_cputime (struct task_struct *tsk,struct task_cputime *times) +void thread_group_cputime (struct task_struct *tsk,bool use_delta,struct task_cputime *times) {struct signal_struct *sig = tsk->signal;cputime_t utime, stime;
@@ -313,7 +313,7 @@ void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)task_cputime (t, &utime, &stime); times->utime += utime; times->stime += stime; - times->sum_exec_runtime +=task_sched_runtime (t); + times->sum_exec_runtime +=task_sched_runtime (t, use_delta); }while_each_thread (tsk, t); out:rcu_read_unlock ();
@@ -459,7 +459,7 @@ void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime {struct task_cputime cputime; -thread_group_cputime (p, &cputime); +thread_group_cputime (p, true, &cputime); *ut = cputime.utime; *st = cputime.stime;
@@ -594,7 +594,7 @@ void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime {struct task_cputime cputime; -thread_group_cputime (p, &cputime); +thread_group_cputime (p, true, &cputime);cputime_adjust (&cputime, &p->signal->prev_cputime, ut, st); }#endif /* !CONFIG_VIRT_CPU_ACCOUNTING */
Signature · 1 line
1.7.1