public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Stanislaw Gruszka <sgruszka@redhat.com>
Cc: "Ingo Molnar" <mingo@elte.hu>,
	"Hidetoshi Seto" <seto.hidetoshi@jp.fujitsu.com>,
	"Américo Wang" <xiyou.wangcong@gmail.com>,
	linux-kernel@vger.kernel.org, "Oleg Nesterov" <oleg@redhat.com>,
	"Spencer Candland" <spencer@bluehost.com>,
	"Balbir Singh" <balbir@in.ibm.com>
Subject: Re: [PATCH] sys_times: fix utime/stime decreasing on thread exit
Date: Fri, 13 Nov 2009 14:16:59 +0100	[thread overview]
Message-ID: <1258118219.22655.203.camel@laptop> (raw)
In-Reply-To: <20091113124235.GA26815@dhcp-lab-161.englab.brq.redhat.com>

On Fri, 2009-11-13 at 13:42 +0100, Stanislaw Gruszka wrote:
> When we have lots of exiting thread, two consecutive calls to sys_times()
> can show utime/stime values decrease. This can be showed by program
> provided in this thread:
> 
> http://lkml.org/lkml/2009/11/3/522
> 
> We have two bugs related with this problem, both need to be fixed to make
> issue gone.
> 
> Problem 1) Races between thread_group_cputime() and __exit_signal()
> 
> When process exit in the middle of thread_group_cputime() loop, {u,s}time
> values will be accounted twice. One time - in all threads loop, second - in
> __exit_signal(). This make sys_times() return values bigger then they
> are in real. Next consecutive call to sys_times() return correct values,
> so we have {u,s}time decrease.
> 
> To fix use sighand->siglock in do_sys_times().
> 
> Problem 2) Using adjusted stime/utime values in __exit_signal()
> 
> Adjusted task_{u,s}time() functions can return smaller values then
> corresponding tsk->{s,u}time. So when thread exit, thread {u/s}times
> values accumulated in signal->{s,u}time can be smaller then
> tsk->{u,s}times previous accounted in thread_group_cputime() loop.
> Hence two consecutive sys_times() calls can show decrease.
> 
> To fix we use pure tsk->{u,s}time values in __exit_signal(). This mean
> reverting:
> 
> commit 49048622eae698e5c4ae61f7e71200f265ccc529
> Author: Balbir Singh <balbir@linux.vnet.ibm.com>
> Date:   Fri Sep 5 18:12:23 2008 +0200
> 
>     sched: fix process time monotonicity
> 
> which is also fix for some utime/stime decreasing issues. However
> I _believe_ issues which want to be fixed in this commit, was caused
> by Problem 1) and this patch not make them happen again.

It would be very good to verify that believe and make it a certainty.

Otherwise we need to do the opposite and propagate task_[usg]time() to
all other places... :/

/me quickly stares at fs/proc/array.c:do_task_stat(), which is what top
uses to get the times..

That simply uses thread_group_cputime() properly under siglock and would
thus indeed require the use of task_[usg]time() in order to avoid the
stupid hiding 'exploit'..

Oh bugger,.. 

I think we do indeed need something like the below, not sure if all
task_[usg]time() calls are now under siglock, if not they ought to be,
otherwise there's a race with them updating p->prev_[us]time.


---

---diff --git a/kernel/posix-cpu-timers.c b/kernel/posix-cpu-timers.c
index 5c9dc22..9b1d715 100644
--- a/kernel/posix-cpu-timers.c
+++ b/kernel/posix-cpu-timers.c
@@ -170,11 +170,11 @@ static void bump_cpu_timer(struct k_itimer *timer,
 
 static inline cputime_t prof_ticks(struct task_struct *p)
 {
-	return cputime_add(p->utime, p->stime);
+	return cputime_add(task_utime(p), task_stime(p));
 }
 static inline cputime_t virt_ticks(struct task_struct *p)
 {
-	return p->utime;
+	return task_utime(p);
 }
 
 int posix_cpu_clock_getres(const clockid_t which_clock, struct timespec
*tp)
@@ -248,8 +248,8 @@ void thread_group_cputime(struct task_struct *tsk,
struct task_cputime *times)
 
 	t = tsk;
 	do {
-		times->utime = cputime_add(times->utime, t->utime);
-		times->stime = cputime_add(times->stime, t->stime);
+		times->utime = cputime_add(times->utime, task_utime(t));
+		times->stime = cputime_add(times->stime, task_stime(t));
 		times->sum_exec_runtime += t->se.sum_exec_runtime;
 
 		t = next_thread(t);
@@ -517,7 +517,8 @@ static void cleanup_timers(struct list_head *head,
 void posix_cpu_timers_exit(struct task_struct *tsk)
 {
 	cleanup_timers(tsk->cpu_timers,
-		       tsk->utime, tsk->stime, tsk->se.sum_exec_runtime);
+		       task_utime(tsk), task_stime(tsk),
+		       tsk->se.sum_exec_runtime);
 
 }
 void posix_cpu_timers_exit_group(struct task_struct *tsk)
@@ -525,8 +526,8 @@ void posix_cpu_timers_exit_group(struct task_struct
*tsk)
 	struct signal_struct *const sig = tsk->signal;
 
 	cleanup_timers(tsk->signal->cpu_timers,
-		       cputime_add(tsk->utime, sig->utime),
-		       cputime_add(tsk->stime, sig->stime),
+		       cputime_add(task_utime(tsk), sig->utime),
+		       cputime_add(task_stime(tsk), sig->stime),
 		       tsk->se.sum_exec_runtime + sig->sum_sched_runtime);
 }
 
@@ -1365,8 +1366,8 @@ static inline int fastpath_timer_check(struct
task_struct *tsk)
 
 	if (!task_cputime_zero(&tsk->cputime_expires)) {
 		struct task_cputime task_sample = {
-			.utime = tsk->utime,
-			.stime = tsk->stime,
+			.utime = task_utime(tsk),
+			.stime = tsak_stime(tsk),
 			.sum_exec_runtime = tsk->se.sum_exec_runtime
 		};
 




  reply	other threads:[~2009-11-13 13:17 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-04  0:23 utime/stime decreasing on thread exit Spencer Candland
2009-11-04  6:49 ` Hidetoshi Seto
2009-11-05  5:24   ` Hidetoshi Seto
2009-11-09 14:49     ` Peter Zijlstra
2009-11-09 17:20       ` Oleg Nesterov
2009-11-09 17:27         ` Oleg Nesterov
2009-11-09 17:31         ` Peter Zijlstra
2009-11-09 19:23           ` Oleg Nesterov
2009-11-09 19:32             ` Peter Zijlstra
2009-11-10 10:44             ` Stanislaw Gruszka
2009-11-10 17:40               ` Oleg Nesterov
2009-11-10 18:24                 ` Stanislaw Gruszka
2009-11-10 19:23                   ` Oleg Nesterov
2009-11-17 12:48                     ` Stanislaw Gruszka
2009-11-17 12:57                       ` [PATCH] posix-cpu-timers: reset expire cache when no timer is running Stanislaw Gruszka
2009-11-10  5:42       ` utime/stime decreasing on thread exit Hidetoshi Seto
2009-11-10  5:47         ` [PATCH] fix granularity of task_u/stime() Hidetoshi Seto
2009-11-11 12:11           ` Stanislaw Gruszka
2009-11-12  0:00             ` Hidetoshi Seto
2009-11-12  2:49               ` Hidetoshi Seto
2009-11-12  2:55                 ` Américo Wang
2009-11-12  4:16                   ` Hidetoshi Seto
2009-11-12  4:33                     ` [PATCH] fix granularity of task_u/stime(), v2 Hidetoshi Seto
2009-11-12 14:15                       ` Peter Zijlstra
2009-11-12 14:49                       ` Stanislaw Gruszka
2009-11-12 15:00                         ` Peter Zijlstra
2009-11-12 15:40                           ` Stanislaw Gruszka
2009-11-13 12:42                             ` [PATCH] sys_times: fix utime/stime decreasing on thread exit Stanislaw Gruszka
2009-11-13 13:16                               ` Peter Zijlstra [this message]
2009-11-13 14:12                                 ` Balbir Singh
2009-11-13 15:36                                 ` Stanislaw Gruszka
2009-11-13 17:05                                   ` Peter Zijlstra
2009-11-16 19:32                             ` [PATCH] fix granularity of task_u/stime(), v2 Spencer Candland
2009-11-17 13:08                               ` Stanislaw Gruszka
2009-11-17 13:24                                 ` Peter Zijlstra
2009-11-19 18:17                                   ` Stanislaw Gruszka
2009-11-20  2:00                                     ` Hidetoshi Seto
2009-11-23 10:09                                       ` Stanislaw Gruszka
2009-11-23 10:16                                         ` [PATCH] cputime: avoid do_sys_times() races with __exit_signal() Stanislaw Gruszka
2009-11-30  9:20                                           ` [PATCH 1/2] cputime: remove prev_{u,s}time if VIRT_CPU_ACCOUNTING Hidetoshi Seto
2009-11-30  9:21                                           ` [PATCH 2/2] cputime: introduce thread_group_times() Hidetoshi Seto
2009-11-30 14:54                                             ` Stanislaw Gruszka
2009-12-01  1:02                                               ` Hidetoshi Seto
2009-12-02  8:26                                           ` [PATCH -v2 1/2] sched, cputime: cleanups related to task_times() Hidetoshi Seto
2009-12-02 15:17                                             ` Peter Zijlstra
2009-12-02 15:29                                               ` Balbir Singh
2009-12-03  0:21                                                 ` Hidetoshi Seto
2009-12-02 15:57                                             ` Peter Zijlstra
2009-12-02 17:33                                             ` [tip:sched/core] sched, cputime: Cleanups " tip-bot for Hidetoshi Seto
2009-12-02  8:28                                           ` [PATCH -v2 2/2] sched, cputime: introduce thread_group_times() Hidetoshi Seto
2009-12-02 15:58                                             ` Peter Zijlstra
2009-12-02 17:33                                             ` [tip:sched/core] sched, cputime: Introduce thread_group_times() tip-bot for Hidetoshi Seto
2009-12-02  8:29                                           ` reproducer: utime decreasing Hidetoshi Seto
2009-12-02  8:32                                           ` reproducer: invisible utime Hidetoshi Seto
2009-11-23 10:25                                         ` [PATCH] fix granularity of task_u/stime(), v2 Balbir Singh
2009-11-23 10:46                                           ` Stanislaw Gruszka
2009-11-24  5:33                                         ` Hidetoshi Seto
2009-11-18 22:38                                 ` Spencer Candland
2009-11-23  9:52                         ` Stanislaw Gruszka
2009-11-12 18:12                       ` [tip:sched/core] sched: Fix granularity of task_u/stime() tip-bot for Hidetoshi Seto
2009-11-13  9:40                         ` Stanislaw Gruszka
2009-11-13 23:09                         ` Ingo Molnar
2009-11-16  2:44                           ` Hidetoshi Seto

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1258118219.22655.203.camel@laptop \
    --to=peterz@infradead.org \
    --cc=balbir@in.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=oleg@redhat.com \
    --cc=seto.hidetoshi@jp.fujitsu.com \
    --cc=sgruszka@redhat.com \
    --cc=spencer@bluehost.com \
    --cc=xiyou.wangcong@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox