[PATCH] posix-timers: CLOCK_MONOTONIC_RAW: fix the usage of->it_clock

From: Oleg Nesterov
Date: Sat Dec 20 2008 - 12:50:19 EST


(compile tested)

common_timer_create() and common_timer_set() blindly pass ->it_clock to
hrtimer_init() as clock_id. This used to work until CLOCK_MONOTONIC_RAW
was introduced, now we should be careful.

Perhaps it makes sense to add BUG_ON(clock_id >= HRTIMER_MAX_CLOCK_BASES)
to __hrtimer_init(), the wrong clock_id leads to catastrophe.

Reported-by: Eric Sesterhenn <snakebyte@xxxxxx>
Signed-off-by: Oleg Nesterov <oleg@xxxxxxxxxx>

--- K-28/kernel/posix-timers.c~CLOCK_MONOTONIC_RAW 2008-12-02 17:12:40.000000000 +0100
+++ K-28/kernel/posix-timers.c 2008-12-20 18:23:28.000000000 +0100
@@ -191,12 +191,20 @@ static inline int common_clock_set(const
return do_sys_settimeofday(tp, NULL);
}

-static int common_timer_create(struct k_itimer *new_timer)
+static inline int
+__common_timer_init(struct k_itimer *timer, enum hrtimer_mode mode)
{
- hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
+ clockid_t clock_id = timer->it_clock ?
+ CLOCK_MONOTONIC : CLOCK_REALTIME;
+ hrtimer_init(&timer->it.real.timer, clock_id, mode);
return 0;
}

+static int common_timer_create(struct k_itimer *new_timer)
+{
+ return __common_timer_init(new_timer, HRTIMER_MODE_ABS);
+}
+
/*
* Return nonzero if we know a priori this clockid_t value is bogus.
*/
@@ -730,7 +738,7 @@ common_timer_set(struct k_itimer *timr,
return 0;

mode = flags & TIMER_ABSTIME ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
- hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
+ __common_timer_init(timr, mode);
timr->it.real.timer.function = posix_timer_fn;

hrtimer_set_expires(timer, timespec_to_ktime(new_setting->it_value));

--
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/