All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] posixtimers: Fix posix clock monotonicity
@ 2009-03-17  6:13 Hidetoshi Seto
  2009-03-17  6:16 ` KAMEZAWA Hiroyuki
  2009-03-18 10:41 ` [PATCH] posixtimers: Fix posix clock monotonicity v2 Hidetoshi Seto
  0 siblings, 2 replies; 13+ messages in thread
From: Hidetoshi Seto @ 2009-03-17  6:13 UTC (permalink / raw)
  To: linux-kernel; +Cc: stable

This patch rehires task_sched_runtime() and thread_group_sched_runtime()
which were removed at the time of 2.6.28-rc1.

These functions protect the sampling of clock with rq lock.
This rq lock is required not to update rq->clock during the sampling.
i.e. You may get ((banked runtime before update)+(delta after update)).

Signed-off-by: Hidetoshi Seto <seto.hidetoshi@jp.fujitsu.com>
Cc: stable@kernel.org	[2.6.28.x]

---
 kernel/posix-cpu-timers.c |    7 +++--
 kernel/sched.c            |   58 +++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 55 insertions(+), 10 deletions(-)

diff --git a/kernel/posix-cpu-timers.c b/kernel/posix-cpu-timers.c
index 4e5288a..a65641a 100644
--- a/kernel/posix-cpu-timers.c
+++ b/kernel/posix-cpu-timers.c
@@ -294,7 +294,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 = p->se.sum_exec_runtime + task_delta_exec(p);
+		cpu->sched = task_sched_runtime(p);
 		break;
 	}
 	return 0;
@@ -310,18 +310,19 @@ static int cpu_clock_sample_group(const clockid_t which_clock,
 {
 	struct task_cputime cputime;
 
-	thread_group_cputime(p, &cputime);
 	switch (CPUCLOCK_WHICH(which_clock)) {
 	default:
 		return -EINVAL;
 	case CPUCLOCK_PROF:
+		thread_group_cputime(p, &cputime);
 		cpu->cpu = cputime_add(cputime.utime, cputime.stime);
 		break;
 	case CPUCLOCK_VIRT:
+		thread_group_cputime(p, &cputime);
 		cpu->cpu = cputime.utime;
 		break;
 	case CPUCLOCK_SCHED:
-		cpu->sched = cputime.sum_exec_runtime + task_delta_exec(p);
+		cpu->sched = thread_group_sched_runtime(p);
 		break;
 	}
 	return 0;
diff --git a/kernel/sched.c b/kernel/sched.c
index db66874..617d1b8 100644
--- a/kernel/sched.c
+++ b/kernel/sched.c
@@ -4066,7 +4066,23 @@ EXPORT_PER_CPU_SYMBOL(kstat);
 /*
  * Return any ns on the sched_clock that have not yet been banked in
  * @p in case that task is currently running.
+ *
+ * Called with task_rq_lock() held on @rq.
  */
+static u64 __task_delta_exec(struct task_struct *p, struct rq *rq)
+{
+	u64 ns = 0;
+
+	if (task_current(rq, p)) {
+		update_rq_clock(rq);
+		ns = rq->clock - p->se.exec_start;
+		if ((s64)ns < 0)
+			ns = 0;
+	}
+
+	return ns;
+}
+
 unsigned long long task_delta_exec(struct task_struct *p)
 {
 	unsigned long flags;
@@ -4074,16 +4090,44 @@ unsigned long long task_delta_exec(struct task_struct *p)
 	u64 ns = 0;
 
 	rq = task_rq_lock(p, &flags);
+	ns = __task_delta_exec(p, rq);
+	task_rq_unlock(rq, &flags);
 
-	if (task_current(rq, p)) {
-		u64 delta_exec;
+	return ns;
+}
 
-		update_rq_clock(rq);
-		delta_exec = rq->clock - p->se.exec_start;
-		if ((s64)delta_exec > 0)
-			ns = delta_exec;
-	}
+/*
+ * Return p->sum_exec_runtime plus any more ns on the sched_clock
+ * that have not yet been banked in case the task is currently running.
+ */
+unsigned long long task_sched_runtime(struct task_struct *p)
+{
+	unsigned long flags;
+	struct rq *rq;
+	u64 ns = 0;
+
+	rq = task_rq_lock(p, &flags);
+	ns = p->se.sum_exec_runtime + __task_delta_exec(p, rq);
+	task_rq_unlock(rq, &flags);
+
+	return ns;
+}
 
+/*
+ * Return sum_exec_runtime for the thread group plus any more ns on the
+ * sched_clock that have not yet been banked in case the task is currently
+ * running.
+ */
+unsigned long long thread_group_sched_runtime(struct task_struct *p)
+{
+	struct task_cputime totals;
+	unsigned long flags;
+	struct rq *rq;
+	u64 ns;
+
+	rq = task_rq_lock(p, &flags);
+	thread_group_cputime(p, &totals);
+	ns = totals.sum_exec_runtime + __task_delta_exec(p, rq);
 	task_rq_unlock(rq, &flags);
 
 	return ns;
-- 
1.6.2.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2009-03-23  9:43 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-03-17  6:13 [PATCH] posixtimers: Fix posix clock monotonicity Hidetoshi Seto
2009-03-17  6:16 ` KAMEZAWA Hiroyuki
2009-03-17  6:31   ` Hidetoshi Seto
2009-03-18 10:41 ` [PATCH] posixtimers: Fix posix clock monotonicity v2 Hidetoshi Seto
2009-03-18 10:58   ` Peter Zijlstra
2009-03-18 11:34   ` Ingo Molnar
2009-03-23  5:07     ` Hidetoshi Seto
2009-03-23  5:11     ` [PATCH 1/2] x86: Rename __task_delta_exec() to task_delta_exec_locked() Hidetoshi Seto
2009-03-23  7:57       ` Peter Zijlstra
2009-03-23  9:13         ` Hidetoshi Seto
2009-03-23  9:40           ` [PATCH] posixtimers: Fix posix clock monotonicity v3 Hidetoshi Seto
2009-03-23  9:42             ` Peter Zijlstra
2009-03-23  5:13     ` [PATCH 2/2] posixtimers: Fix posix clock monotonicity v2 Hidetoshi Seto

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.