[PATCH 8/8] posix-cpu-timers: fix cputimer initialization mistake for {u,s}time

From: kosaki . motohiro
Date: Sun May 26 2013 - 17:38:00 EST


From: KOSAKI Motohiro <kosaki.motohiro@xxxxxxxxxxxxxx>

When using CPUCLOCK_VIRT or CPUCLOCK_PROF, we need to round an expire up
because jiffies based accounting may point to one jiffy behind at maximum.
So, current code lead to wake up posix timer too early.

This patch adds one jiffy at timer initialization. itimer already has a
similar trick and it match a userland assumption.

Luckily, CPUCLOCK_VIRT and CPUCLOCK_PROF posix timer are undocumented,
standard undefined, and no libc support. So maybe nobody uses them.
However, it still would be nice to fix if possible.

Cc: Olivier Langlois <olivier@xxxxxxxxxxxxxx>
Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Cc: Frederic Weisbecker <fweisbec@xxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxxxxx>
Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
Signed-off-by: KOSAKI Motohiro <kosaki.motohiro@xxxxxxxxxxxxxx>
---
kernel/posix-cpu-timers.c | 20 ++++++++++++++++++++
1 files changed, 20 insertions(+), 0 deletions(-)

diff --git a/kernel/posix-cpu-timers.c b/kernel/posix-cpu-timers.c
index 34bb3f1..42874c2 100644
--- a/kernel/posix-cpu-timers.c
+++ b/kernel/posix-cpu-timers.c
@@ -339,6 +339,18 @@ static int cpu_timer_sample_group(const clockid_t which_clock,
return do_cpu_clock_timer_sample(which_clock, p, true, false, cpu);
}

+static union cpu_time_count cpu_clock_resolution(clockid_t cpuid)
+{
+ union cpu_time_count ret;
+
+ if (CPUCLOCK_WHICH(cpuid) == CPUCLOCK_SCHED)
+ ret.sched = 1;
+ else
+ ret.cpu = cputime_one_jiffy;
+
+ return ret;
+}
+
static int posix_cpu_clock_get(const clockid_t which_clock, struct timespec *tp)
{
const pid_t pid = CPUCLOCK_PID(which_clock);
@@ -806,7 +818,15 @@ static int posix_cpu_timer_set(struct k_itimer *timer, int flags,
cpu_clock_sample(timer->it_clock, p, &now);
else
cpu_clock_sample_group(timer->it_clock, p, &now);
+
cpu_time_add(timer->it_clock, &new_expires, now);
+
+ /*
+ * When inaccurate timer, we need to adjust an expire time
+ * because "now" may point to slightly past time.
+ */
+ cpu_time_add(timer->it_clock, &new_expires,
+ cpu_clock_resolution(timer->it_clock));
}

/*
--
1.7.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/