hrtimer: generic sleeper infrastructure
cover hrtimer: generic sleeper infrastructure
Andrew,
the removal of the data field in the hrtimer structure introduced a private sleeper implementation for nanosleep. wakeup is the most common callback case for timers and we should use a generic implementation instead of forcing users to reimplement it all over the place.
tglx
--
PATCH 1/2 hrtimer
The removal of the data field in the hrtimer structure enforces the embedding of the timer into another data structure. nanosleep now uses a private implementation of the most common used timer callback function (simple task wakeup). In order to avoid the reimplentation of such functionality all over the place a generic hrtimer_sleeper functionality is created.
include/linux/hrtimer.h | 16 ++++++++++++++++ kernel/hrtimer.c | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+)
include/linux/hrtimer.h +16 -0
@@ -58,6 +58,19 @@ struct hrtimer { };/** +* struct hrtimer_sleeper - simple sleeper structure +* +* @timer: embedded timer structure +* @task: task to wake up +* +* task is set to NULL, when the timer expires. +*/ +struct hrtimer_sleeper { +struct hrtimer timer; +struct task_struct *task; +}; + +/** * struct hrtimer_base - the timer base for a specific clock * * @index: clock type index for per_cpu support when moving a timer
@@ -127,6 +140,9 @@ extern long hrtimer_nanosleep(struct timconst enum hrtimer_mode mode, const clockid_t clockid); +extern void hrtimer_init_sleeper (struct hrtimer_sleeper *sl, +struct task_struct *tsk); +/* Soft interrupt function to run the hrtimer queues: */ extern void hrtimer_run_queues (void );
kernel/hrtimer.c +19 -0
@@ -656,6 +656,25 @@ void hrtimer_run_queues(void)* Sleep related functions: */ +static int hrtimer_wakeup (struct hrtimer *timer) +{ +struct hrtimer_sleeper *t = + container_of(timer, struct hrtimer_sleeper, timer); +struct task_struct *task = t->task; + + t->task =NULL ; +if (task) +wake_up_process (task); + +return HRTIMER_NORESTART ; +} + +void hrtimer_init_sleeper (struct hrtimer_sleeper *sl,task_t *task) +{ + sl->timer.function = hrtimer_wakeup; + sl->task = task; +} +struct sleep_hrtimer {struct hrtimer timer;struct task_struct *task;
--
PATCH 2/2 hrtimer
In reply to Thomas GleixnerReplace the nanosleep private sleeper functionality by the generic hrtimer sleeper.
kernel/hrtimer.c | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-)
kernel/hrtimer.c +7 -27
@@ -655,7 +655,6 @@ void hrtimer_run_queues(void)/* * Sleep related functions: */ -static int hrtimer_wakeup (struct hrtimer *timer) { struct hrtimer_sleeper *t =
@@ -675,28 +674,9 @@ void hrtimer_init_sleeper(struct hrtimer sl->task = task; } -struct sleep_hrtimer { -struct hrtimer timer; -struct task_struct *task; -int expired; -}; - -static int nanosleep_wakeup (struct hrtimer *timer) -{ -struct sleep_hrtimer *t = - container_of(timer, struct sleep_hrtimer, timer); - - t->expired =1 ; -wake_up_process (t->task); - -return HRTIMER_NORESTART ; -} - -static int __sched do_nanosleep(struct sleep_hrtimer *t, enum hrtimer_mode mode) +static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode) { - t->timer.function = nanosleep_wakeup; - t->task = current; - t->expired =0 ; +hrtimer_init_sleeper (t, current); do { set_current_state(TASK_INTERRUPTIBLE);
@@ -704,18 +684,18 @@ static int __sched do_nanosleep(struct sschedule (); -if (unlikely (!t->expired)) { +if (unlikely (t->task)) {hrtimer_cancel (&t->timer); mode =HRTIMER_ABS ; } - }while (!t->expired && !signal_pending (current)); + }while (t->task && !signal_pending (current)); -return t->expired; +return t->task ==NULL ; }static long __sched nanosleep_restart (struct restart_block *restart) { -struct sleep_hrtimer t; +struct hrtimer_sleeper t;struct timespec __user *rmtp;struct timespec tu;ktime_t time;
@@ -748,7 +728,7 @@ long hrtimer_nanosleep(struct timespec * const enum hrtimer_mode mode, const clockid_t clockid) { struct restart_block *restart; -struct sleep_hrtimer t; +struct hrtimer_sleeper t;struct timespec tu;ktime_t rem;
--
Re: [patch 2/2] hrtimer
Quoted 60 lines · Thomas Gleixner
Replace the nanosleep private sleeper functionality by the generic hrtimer sleeper.
Signed-off-by Thomas Gleixner <[email protected]>Signed-off-by Ingo Molnar <[email protected]>kernel/hrtimer.c | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-)Index:
linux-2.6.16/kernel/hrtimer.c
===================================================================kernel/hrtimer.c +2
-23@@ -655,7 +655,6 @@ void hrtimer_run_queues(void)/* * Sleep related functions: */ -static int hrtimer_wakeup (struct hrtimer *timer) { struct hrtimer_sleeper *t =@@ -675,28 +674,9 @@ void hrtimer_init_sleeper(struct hrtimer sl->task = task; } -struct sleep_hrtimer { -struct hrtimer timer; -struct task_struct *task; -int expired; -}; - -static int nanosleep_wakeup (struct hrtimer *timer) -{ -struct sleep_hrtimer *t = - container_of(timer, struct sleep_hrtimer, timer); - - t->expired =1 ; -wake_up_process (t->task); - -return HRTIMER_NORESTART ; -} - -static int __sched do_nanosleep(struct sleep_hrtimer *t, enum hrtimer_mode mode) +static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode) { - t->timer.function = nanosleep_wakeup; - t->task = current; - t->expired =0 ; +hrtimer_init_sleeper (t, current); do { set_current_state(TASK_INTERRUPTIBLE);@@ -704,18 +684,18 @@ static int __sched do_nanosleep(struct sschedule (); -if (unlikely (!t->expired)) {
kernel/hrtimer.c static int __sched do_nanosleep(struct s · line 694
Quoted 10 lines
kernel/hrtimer.c +3
-2+if (unlikely (t->task)) {hrtimer_cancel (&t->timer); mode =HRTIMER_ABS ; } - }while (!t->expired && !signal_pending (current)); + }while (t->task && !signal_pending (current)); -return t->expired; +return t->task ==NULL ; }
This all looks vaguely racy. hrtimer_wakeup() will set t->task to NULL without barriers, locks or anything. And the waiter here can break out of schedule() due to signal delivery while a wakeup is in progress.
So the value of t->task here is fairly meaningless. Ot just depends on how far the waker has got through hrtimer_wakeup().
Maybe that doesn't matter, because hrtimer_cancel() will spin until hrtimer_wakeup() has completed anyway, but could you please recheck and confirm that this is all solid?
Re: [patch 2/2] hrtimer
Quoted 3 lines · Andrew Morton
This all looks vaguely racy.
hrtimer_wakeup()will sett->taskto NULL without barriers, locks or anything. And the waiter here can break out ofschedule()due to signal delivery while a wakeup is in progress.
We set task = NULL before wake_up_process() which acts as a barrier.
Quoted 6 lines
So the value of
t->taskhere is fairly meaningless. Ot just depends on how far the waker has got throughhrtimer_wakeup().Maybe that doesn't matter, because
hrtimer_cancel()will spin untilhrtimer_wakeup()has completed anyway, but could you please recheck and confirm that this is all solid?
Right, either it waits for the running timer or in case the wakeup happens between
if (unlikely(t->task)) {
and
hrtimer_cancel(&t->timer);
then hrtimer_cancel will see that the timer is inactive and we drop out of the loop because the while(t->task) condition is not longer true.
tglx
Re: [patch 2/2] hrtimer
I also think this is racy.
CPU_0 CPU_1
hrtimer_wakeup:
task = t->task; t->task = NULL; <--- INTERRUPT ---> task is woken by signal, do_nanosleep() sees t->task == NULL, returns without hrtimer_cancel(), and __exits__. <--- RESUME ---> wake_up_process(task);
Instead of exit(), 'task' can go to TASK_STOPPED or TASK_UNINTERRUPTIBLE after return from do_nanosleep(), it will be awakened by hrtimer_wakeup() unexpectedly.
Oleg.
Re: [patch 2/2] hrtimer
Hi,
Quoted 20 lines · Oleg Nesterov
I also think this is racy.
CPU_0CPU_1
hrtimer_wakeup:task = t->task; t->task = NULL; <--- INTERRUPT ---> task is woken by signal, do_nanosleep() sees t->task == NULL, returns without hrtimer_cancel(), and __exits__. <--- RESUME ---> wake_up_process(task);
Quoted 3 lines
Instead of
exit(), 'task' can go toTASK_STOPPEDorTASK_UNINTERRUPTIBLEafter return fromdo_nanosleep(), it will be awakened byhrtimer_wakeup()unexpectedly.
Indeed and my original patch did call hrtimer_cancel() unconditionally to synchronize with a possibly running timer.
Thomas, could you please document it a bit better, when you modify my patches?
bye, Roman
Re: [patch 2/2] hrtimer
In reply to Oleg NesterovQuoted 3 lines · Oleg Nesterov
Instead of
exit(), 'task' can go toTASK_STOPPEDorTASK_UNINTERRUPTIBLEafter return fromdo_nanosleep(), it will be awakened byhrtimer_wakeup()unexpectedly.
Yep, you are right.
kernel/hrtimer.c +3 -4
@@ -684,10 +684,9 @@ static int __sched do_nanosleep(struct hschedule (); -if (unlikely (t->task)) { -hrtimer_cancel (&t->timer); - mode =HRTIMER_ABS ; - } +hrtimer_cancel (&t->timer); + mode =HRTIMER_ABS ; + }while (t->task && !signal_pending (current));return t->task ==NULL ;